jQuery .each循环并替换到同一位置? [英] jquery .each loop and replace to same location?

查看:139
本文介绍了jQuery .each循环并替换到同一位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

代码:

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?

        });

推荐答案

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

这篇关于jQuery .each循环并替换到同一位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆