0

我得到了帮助(使用 jQuery 从文本变量中选择带有选择器的标签)循环静态 var 并替换它的值,但只剩下一个问题,是如何用文本中新更改的标签替换找到的标签区域

代码:

var length = 30;
var end    = '...';
var text = `some string here with <a href="#link">http:something.com</a> more string and more links also`;

$('<div>' + text + '</div>').find('a').each(function() {

                var link_value = $(this).html();
        $(this).html(link_value.substring(0, length-1)+(link_value.length > length ? end : ''));
// now how can i put $(this).html() back in the text area, which it was found at?

        });
4

2 回答 2

1

实际上,当以一种或另一种方式进行更改时,会进行更改,您无需将其放回原处,只需使用 end()

var div = $('<div>' + text + '</div>').find('a').each(function() {...}).end();
于 2009-10-12T11:47:04.680 回答
0
    var length = 30;
    var end    = '...';
    var div = $('<div>' + text + '</div>');
    $(div).find('a').each(function() {
        var link_value = $(this).html();
        $(this).html(link_value.substring(0, length)+(link_value.length > length ? end : ''));
    });

    var text = div.html();
于 2009-10-12T11:44:12.303 回答