2013-04-25 55 views
2

我想改變的文本進行替換「您未被授權訪問」,在下面的代碼(訪問被拒絕請登錄或註冊。): -一個孩子在父母DIV通過JQuery的

<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 ... 
+0

錯誤,請解釋一下你的 「它不能正常工作」 的意思。 – 2013-04-25 16:53:32

+0

這不是取代文字。基本上「不做」我在我的問題中寫的。 – Steve 2013-04-25 16:57:58

回答

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

這樣做....非常感謝! – Steve 2013-04-25 16:57:24

+0

實際上,不,它沒有......它可以工作,但出現如下所示的錯誤。 – Steve 2013-04-29 18:49:33