jquery .each loop and replace to same location?

醉酒当歌 提交于 2019-12-12 04:58:36

问题


i got help (Select a Tag with a Selector from a Text Variable using jQuery) on looping the static var and replacing it value, but just one question is left from it, is how can i replace finded tags with newly changed tags in the text area

Code:

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?

        });

回答1:


Actually when changing this one way or the other, the changes are made and you don't need to put it back instead just use end()

var div = $('<div>' + text + '</div>').find('a').each(function() {...}).end();



回答2:


    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();


来源:https://stackoverflow.com/questions/1553784/jquery-each-loop-and-replace-to-same-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!