我有一個名爲array的數組(註釋它是TOP數組),這個數組實際上包含不同div元素的類。我加入了這個數組,讓這個表單的每個元素爲「.1」或「.4」等等,現在如果任何一個類都被點擊了,我將它的html存儲在變量hello中,然後我從hello的html中提取類並把它們放入一個數組中。現在我需要知道用戶點擊了什麼,所以我做了一個數組blaharray來比較hello的數組元素。通過這樣做,我試圖通過blaharray匹配類似「1」或「6」的hello數組。如果匹配,我添加「。」用hello的匹配元素使它像「.7」一樣表示用戶單擊的類,並將其存儲在userclicked中。但是,如果單擊「.1」類,則userclicked不會返回任何內容。所以我試着檢查hello數組中是否存在類「.1」中的問題,並發現在for循環之外(瘋狂代碼開始),hello返回類「.1」的正確數組,例如[「1」, 「col-xs-4」,「col-md-4」]但是當我點擊「.1」的時候,在控制檯問候時,它會在控制檯內輸出。但是如果我點擊2到9之類的任何其他類,它會給出適當的hello和userclicked輸出內外循環。但是,當hello是[「1」,「col-xs-4」,「col-md -4「](我的意思是當」.1「被點擊時)?爲什麼它不適用於數組中的「.1」類,並且對其他類正常工作?
array = [".1,.2,.3,.4,.5,.6,.7,.8,.9"]; //TOP array
$(array.join("")).click(function() {
$(this).html(input);
var hello=this; //it will store the html of clicked class
hello=hello.className.split(/\s+/); //it will extract the classes and put them in an array like this---> ["1", "col-xs-4", "col-md-4"]
var blaharray=["1","2","3","4","5","6","7","8","9"];
console.log(hello); //here it works giving ["1", "col-xs-4", "col-md-4"]
for(var i=0;i<hello.length;i++){ //Crazy code starts
if(blaharray.indexOf(hello[i])>0){ //it will check if any class in hello array is found in blah array,we are checking here for class "1" of ["1", "col-xs-4", "col-md-4"]
userclicked="."+hello[i]; //if it is found, it adds a "." to it to make ".1" of TOP array joined by ""(I will use it in some code)
console.log(hello); //here the console produces nothing i.e blank ONLY if ".1" is clicked
}
} //Crazy code ends to tell us what the user clicked
});
這裏是用於參考的類的類我有像div等1,2等類的類。
<div class="container-fluid">
<div class=" yo text-center row">
<h1>Tic Tac Toe by Uzma</h1>
<div class="col-xs-12 col-md-12 we">
<div class="row ">
<div class="1 col-xs-4 col-md-4"></div>
<div class="2 col-xs-4 col-md-4"></div>
<div class="3 col-xs-4 col-md-4"></div>
</div>
<div class="row">
<div class="4 col-xs-4 col-md-4"></div>
<div class="5 col-xs-4 col-md-4"></div>
<div class="6 col-xs-4 col-md-4"></div>
</div>
<div class="row">
<div class="7 col-xs-4 col-md-4"></div>
<div class="8 col-xs-4 col-md-4"></div>
<div class="9 col-xs-4 col-md-4"></div>
</div>
</div>
</div>
</div>
這裏是Codepen檢查不同的點擊http://codepen.io/meow414/pen/QExLOG?editors=1001
您的類名必須以字母,連字符或下劃線開頭。不是數字。 –
這不是重點。對於其他NUMBERS類,它工作正常。 –
你不需要頂部的數組。這只是一個單一的字符串,而不是幾個數字。或者只是丟掉引號,使其成爲一組適當的數字。 – ManoDestra