通过JQuery替换父级DIV下的子级中的文本 [英] Replacing Text in a child under parent DIV through JQuery

查看:116
本文介绍了通过JQuery替换父级DIV下的子级中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将(拒绝访问.请登录或注册.)中的文本更改为 您无权访问" 以下代码:-

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>

我尝试在Ready Function中使用以下代码,但无法正常工作.

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"); 
    });

谢谢.

还有其他方法可以做到吗?它给了我

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

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

推荐答案

$("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";

这篇关于通过JQuery替换父级DIV下的子级中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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