2012-04-11 74 views
0

我寫一些代碼,需要訪問含有特定文本 下一個div div的數量的代碼的小部分我的工作:獲得包含特定文本

var txtElem = txtdiv.getElementsByTagName("div"); 
txtElem[9].style.border = "2px solid blue"; 

爲見上面我正在訪問索引號的特定股利,但現在我想添加更多的代碼,可以返回我從TxtElem的索引,其中包含從頁面

+0

代碼你能不能給一些標記嗎? – 2012-04-11 09:43:44

回答

1

嘗試以下

var txtElem = txtdiv.getElementsByTagName("div"); 

for (var i = 0; i < txtElem.length; i++) { 
    if(txtElem[i].innerHTML === "The text in the div") { 
     //i is the index of the div that contains the text you searched on 
     alert(i); 
    } 
} 
+0

我可以比較用戶通過代碼選擇的文本嗎? – hiteshtr 2012-04-11 09:51:48

2

選定的文本您需要循環槽,並檢查contentHTML內容

+0

allas你打我:) – buymypies 2012-04-11 09:47:30

+0

確定但我如何得到用戶選擇的文本 – hiteshtr 2012-04-11 09:48:35

+0

@hiteshtr,如果你想獲得用戶的選擇,你可能想看看這個線程:http://stackoverflow.com/questions/845390/javascript-to-get-paragraph-of-selected-text-in-web-page – 2012-04-11 09:56:29

0

通過div循環並使用JQuery.html()檢查div元素的html內容。

0

爲了方便使用,你可能想使用jquery看到http://api.jquery.com/contains-selector/

+0

但這種方法不會返回存儲在txtElem中的我的div的索引 – hiteshtr 2012-04-11 09:52:49

+0

但是,如果存儲它,則有一個對該節點的引用在一個變種,所以你可以添加你的CSS到.css方法不適合? – 2012-04-11 10:07:47

相關問題