javascript - 在 jQuery 中查找/替换 HTML 标签之间的字符串?

标签 javascript jquery html xml

<分区>

假设我有以下代码:

<div>
    Some text
    <h1>A divider</h1>
    More text
</div>

[最好] 通过 jQuery,我如何获取“更多文本”并替换它?

最佳答案

jQuery 并不能很好地支持文本节点,但是像 nextSibling 这样的原生 JS 支持:

$('div h1').get(0).nextSibling.nodeValue = 'New text';

FIDDLE

关于javascript - 在 jQuery 中查找/替换 HTML 标签之间的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16948850/

相关文章:

javascript - 某些 Android 设备/Chrome 上的 HTML5 数字输入中没有小数位

javascript - React hook 形式用以前的值注册新字段

javascript - 将文本设置为子节点

jquery - 将 isotope 与 requirejs 和 jquery 一起使用

html - CSS - 具有恒定宽度内容的全宽粘性页脚

javascript - 如何在 Visual Studio 2013 中抑制 JSHint "JSCS: Illegal Space"警告?

javascript - 这个表达式在 javascript 中是什么意思?

javascript - jQuery 复选框错误

html - 将样式应用于包含嵌套列表的列表项

javascript - html5错误使用IE8