JQuery更改内部文本但保留html

Dre*_*ejc 62 jquery

我想更改HTML元素的文本,但使用jQuery保留内部html的其余部分.

例如:

<a href="link.html">Some text <img src="image.jpg" /></a> 
Run Code Online (Sandbox Code Playgroud)

将"Some text"替换为"Other text",结果应如下所示:

<a href="link.html">Other text <img src="image.jpg" /></a> 
Run Code Online (Sandbox Code Playgroud)

编辑:我目前的解决方案如下:

var aElem = $('a');
var children = aElem.children();

aElem.text("NEW TEXT");
aElem.append(children); 
Run Code Online (Sandbox Code Playgroud)

但必须有一些更优雅的方式来做到这一点.

Bra*_*one 34

这似乎工作得很好.

Live Demo

<html>
    <head>
    </head>
    <body>
        <a href="link.html">Some text <img src="img.jpg" /></a>
    </body>

    </html>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            var $link = $('a');
            var $img = $link.find('img'); 
            $link.html('New Text');
            $link.append($img);
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

  • 是的,这类似于我当前的解决方案。我改用children()属性,然后将子级重新插入到元素中。这工作正常,但我想知道是否有更好的解决方案。 (2认同)
  • 只要您没有将任何事件侦听器绑定到“&lt;a&gt;”标记内的 HTML,此解决方案就可以正常工作。否则,您将必须再次添加这些侦听器。 (2认同)

Mar*_*man 32

由于您无法修改链接,因此只需使用选项即可 replace

$("a").html($("a").html().replace("Some text", "Other text"));
Run Code Online (Sandbox Code Playgroud)

关于jsfiddle的例子.

  • 如果`<img>`标签包含`some text` ?? (4认同)
  • 为什么投票失败?使用此方法将保留原始标记.防爆.`<a> <img rel="nofollow noreferrer" />带有分离/重新附加的一些文本<img rel="nofollow noreferrer" /> </a>`导致`<a>其他文本<img rel="nofollow noreferrer" /> <img rel="nofollow noreferrer" /> </a>`使用简单的替换维护结构. (3认同)
  • 有点天真,对吗?我把它投了下来,因为显然字符串"Some text"不会是部署时被替换的字符串.这当然是动态网站中的其他字符串.因此,不可能依赖这样的字符串匹配. (3认同)

meo*_*ouw 20

在跨度中包装要更改的文本

<a href="link.html"><span>Some text</span> <img src="image.jpg" /></a> 

$('a span').html( 'new text' );
Run Code Online (Sandbox Code Playgroud)

  • 不能这样做,因为元素是从其他来源动态构建的. (2认同)
  • 然而,这是大多数人的最佳答案. (2认同)

And*_*ong 11

我的解决方案,虽然有点晚.

$('a').each(function() {
    var contents = $(this).contents();
    if (contents.length > 0) {
        if (contents.get(0).nodeType == Node.TEXT_NODE) {
            $(this).html('NEW TEXT').append(contents.slice(1));
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

一些说明:

  1. contents()包括文本节点,不像children().
  2. 有必要通过创建内容的副本,var contents = ...否则其余元素一旦被替换就会永久丢失$(this).html('NEW TEXT').
  3. length检查是可选的,但建议使用.
  4. nodeType检查是可选的,但建议使用.

  • 这似乎是理想的选择。这也可以: $(this).contents().filter(function() { return this.nodeType == 3; }).each( function() { this.nodeValue = "whatever"; }) (2认同)

jim*_*rin 10

另一种方法是使用contents()如下方法:

特定

<a href="link.html">Some text <img src="image.jpg" /></a>
Run Code Online (Sandbox Code Playgroud)

你可以'Some text'用jQuery 替换

var text = $('a').contents().first()[0].textContent;
$('a').contents().first()[0].textContent = text.replace("Some text", "Other text");
Run Code Online (Sandbox Code Playgroud)

这里是 jsFiddle的例子.

这个想法contents()受到了这个问题的启发,用户charlietfl回答道.

  • 这似乎是最完整的答案,因为绑定到子HTML节点的任何事件侦听器都不会受到更改的影响. (2认同)

小智 7

您可以通过.contents()进行更改

$('.yourElement').contents()[0].data = 'New Data';
Run Code Online (Sandbox Code Playgroud)

在您的情况下,“ [0] .data”是您的文本数据

  • 最好的解决方案在这里,非常感谢!不会影响任何绑定到 html 的事件侦听器。 (2认同)