Replacing Text in a child under parent DIV through JQuery

点点圈 提交于 2020-01-11 10:54:26

问题


I want to change text of (Access denied. Please Login or Register.) to "You are not authorized to access" in the following code:-

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

I tried using the following code in on Ready Function, but it isn't working correctly.

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

Thanks.


Is there anyother way to do that? It gives me the error of

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

回答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";


来源:https://stackoverflow.com/questions/16220456/replacing-text-in-a-child-under-parent-div-through-jquery

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