通过JQuery的下父DIV在孩子替换文本(Replacing Text in a child un

2019-09-01 03:56发布

我想改变的文字下面的代码为“您没有权限访问”( 访问被拒绝请登录或注册。): -

<div class="messages error">
<h2 class="element-invisible">Error message</h2>
Access denied. Please Login or Register.
</div>

我试着在就绪功能使用下面的代码,但它不能正常工作。

$("div.messages").text(function () {
        console.log($(this).text());
        return $(this).text().replace("Access denied. Please Login or Register.", "You are not authorized to access"); 
    });

谢谢。


有没有任何其它办法做到这一点? 它给我的错误

Uncaught TypeError: Cannot set property 'nodeValue' of undefined ... 

Answer 1:

$("div.messages h2")
      .prop('nextSibling')
      .nodeValue = "You are not authorized to access";

http://jsfiddle.net/ZwLKZ/

$("div.messages").contents().filter(function() {
     return this.nodeType === 3 && $.trim(this.nodeValue) !== ''; 
}).get(0).nodeValue = "You are not authorized to access";


文章来源: Replacing Text in a child under parent DIV through JQuery