-1

我有以下问题。

在 div 中动态添加后,我需要修改锚文本。

在我的代码中:

“加载文件”按钮在 div class="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 回答