我一直在試圖解決這個問題的最後幾個小時,但所有的嘗試都失敗了......更新文本使其在更新之前的DIV的已經點擊 - jQuery的
我需要做什麼是點擊一個會導致另一個DIV彈出的DIV。 彈出裏面有一些<li>
,點擊後,屬性名稱被「轉移」到點擊的第一個DIV。
好吧,我設法做到了這一點,但在我更新第一個DIV後,當我嘗試更新第二個DIV時,第一個DIV也得到更新,當我嘗試更新第三個DIV時,另外兩個更新了屁股。
任何人都可以幫助我解決它,只更新它被點擊離開之前的DIV,因爲它應該是?
下面是代碼:
HTML
<div class="num-1">
<img src="http://dummyimage.com/180x180/eeeeee/000000.jpg" width="180" height="180">
<p class="brand"></p>
<p class="name"></p>
</div>
<div class="num-2">
<img src="http://dummyimage.com/180x180/eeeeee/000000.jpg" width="180" height="180">
<p class="brand"></p>
<p class="name"></p>
</div>
<div class="num-3">
<img src="http://dummyimage.com/180x180/eeeeee/000000.jpg" width="180" height="180">
<p class="brand"></p>
<p class="name"></p>
</div>
<div class="num-4">
<img src="http://dummyimage.com/180x180/eeeeee/000000.jpg" width="180" height="180">
<p class="brand"></p>
<p class="name"></p>
</div>
<div class="num-5">
<img src="http://dummyimage.com/180x180/eeeeee/000000.jpg" width="180" height="180">
<p class="brand"></p>
<p class="name"></p>
</div>
<div class="popup">
<ul>
<li name="{{PHP GENERATED $name}}">{{PHP GENERATED $name}}</li>
<li name="{{PHP GENERATED $name}}">{{PHP GENERATED $name}}</li>
<li name="{{PHP GENERATED $name}}">{{PHP GENERATED $name}}</li>
</ul>
</div>
jQuery的
如果我得到了DIV的數量
//Openning the .popup and assigning the names
//First check the number of the div it was clicked on
$('img').on('click', function() {
var num = $(this).parent().attr('class').match(/\d+/)[0];
$('.popup').fadeIn();
//Click li to update the brand and name on the page
$('.popup li').on('click', function() {
//Check the name and split it
var nameComplete = $(this).attr('name');
var Array = nameComplete.split(" ");
//Check the first word and identify it as brand, also update on the page
var brand = Array[0];
$('.num-' + num + ' .brand').text(brand);
//Check the rest of the array for the name and update it on the page
var name = '';
for(var i=1; i<Array.length ;i++) {
name = name + Array[i] + ' ';
}
name = $.trim(name);
$('.num-' + num + ' .name').text(name);
$('.popup').fadeOut();
});
});
有人可能會問, correctl年。 我做console.log(num);
它表明我在正確的DIV點擊因爲DIV類是num-X
(X = 1〜5),1對1,2 2,...
的<li>
的名稱沒問題,它應該像它應該的那樣工作,將其拆分並獲得「轉移」的正確部分。
我想我做了適當的修改以更好地理解代碼。
你('.div'或''.div'')選擇器在$('。div img')。on('click',function(){...'有一個點,這是想要嗎?你真的想指向一個類div')? – chimos
對不起,我不得不適應它把它放在這裏,我將它更新爲'$('img')'只有。謝謝。 – William
你能提供這個代碼的jsfiddle這個代碼有多個問題如果我們有一個工作js小提琴,它會更容易 –