-1

次の問題があります。

div内に動的に追加された後、アンカーテキストを変更する必要があります。

私のコードでは:

「ファイルのロード」ボタンは、divクラス=「div」にファイルロードリンクを追加します(これはシミュレーションです)。ファイルをロードするプログラムは変更できません。

そのプログラムは、パスとファイル名を含むアンカーテキストを返しますが、名前を保持したいだけです。

問題は、アンカー テキストを変更するコードをどのイベントに配置したかがわからないことです。私はテストを行い、mousemove イベントで動作しますが、単なるテストです。

アンカー テキストを変更するコードをトリガーする方法は?

以下のコードは機能しませんが、mousemove イベントを有効にして、目的の結果を確認できます。

前もって感謝します!あなたの助けに...

$(document).ready(function () {
    $("#btnSave").click(function () {
          // alert("Alert  Click");
    	$('.div').append(
            '<a id="link-x" href="http://example.com/file-3.pdf">http://example.com/file-3.pdf</a><br>');
	});
  
/*    This Works only for Testing
    $(document).mousemove(function () {
        $('a[id*="link"]').each(function(){
            var text = $( this ).text();
            var html = $( this ).html();
            var res = text .replace( 'http://example.com/', '' );
            $( this ).text( res );
        });
	});
*/    
    $('a[id*="link"]').on(function () {
        $('a[id*="link"]').each(function(){
            var text = $( this ).text();
            var html = $( this ).html();
            var res = text .replace( 'http://example.com/', '' );
            $( this ).text( res );
        });
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="mycode">
<button id="btnSave">Load File</button>
</div>
<div class="div">
<a id="link-x" href="http://example.com/file-1.pdf">file-1.pdf</a><br>
<a id="link-x" href="http://example.com/file-2.pdf">file-2.pdf</a><br>
</div>

4

1 に答える 1

0

これを試して:

$(document).ready(function () {
    $("#btnSave").click(function () {
        $('[id*="link"]').text(function() {
            return $(this).text().replace("http://example.com/", "");
        });
    });
});

フィドル

于 2015-08-15T21:06:23.307 に答える