[英]Array function not working on a JavaScript array created from element text
所以我有以下元素,當單擊時調用JavaScript函數response(tag)。 我將元素“ rops”本身傳遞給函數。
<li><div class="rops"><span class="rops-text"> tag2 </span></div></li>
<li><div class="rops"><span class="rops-text"> tag2 </span></div></li>
因此,當我單擊這些元素之一時,會將它們添加到數組中。 但是現在,數組函數適用於該數組,而它們適用於正常創建的數組。 請參閱以下工作案例,而不是工作案例:
如果我自己創建一個數組,它們將起作用:
工作案例:
var checkA = [];
checkA[0] = "tag1";
checkA[1] = "tag2";
alert(checkA.indexOf("tag2")); //Gives me correct answer.
不起作用:數組創建如下:
response(tag) {
myArray[i] = String($(tag).text()); //at current point has ["tag1", "tag2"]
//But if i do the same
alert(myArray.indexOf("tag2")); //Gives me -1 (similar for other array function).
簡而言之,使用元素文本創建數組會導致數組功能無法正常工作。 任何人都可以幫忙。 我不確定為什么會這樣? 謝謝。
嘗試這個:
function result(tag){
myArray = document.getElementsByTagName('span');
alert(myArray.item('tag').innerHTML);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.