2012-03-26 101 views
0

好的功能,所以我得到如何使用屬性,現在我想要做的,是爲jQuery找到屬性即時通訊使用和使用它只一次打開一個評論框使用jQuery來查找一個屬性,然後只做基於屬性

HTML:

<div class="commentopen" id="1">Comment</div> 
<div class="comment" id="1"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="2">Comment</div> 
<div class="comment" id="2"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="3">Comment</div> 
<div class="comment" id="3"><textarea>write a comment...</textarea></div> 

我有類.comment display: none;

的jQuery:

$('.commentopen').click(function() { 
    var id = $(this).attr('id'); 
    $(".comment" + id).slideDown(180, function() { 
     $('#container').isotope('reLayout'); 
    }); 
}); 

我真的可以使用一些幫助,爲什麼它不工作。一直在爲此工作好幾天!

+3

ID應該[HTML文檔中是唯一的(http://www.w3.org/TR/html5/elements.html #在-ID屬性)。你應該重新考慮你的方法。 – Andrew 2012-03-26 15:40:37

+2

圍繞生產中的這段代碼,你有一個document.ready()嗎?此外,像我之前的幾個,小心ID,也許使用您的意見,而不是數據ID ... – jcreamer898 2012-03-26 15:41:14

+0

即時通訊使用MySQL來獲取評論ID的不同職位。 – 2012-03-26 15:46:44

回答

3

你必須重複你的ID(例如,有ID爲1兩種元素),解決通過添加一個前綴:

<div class="comment" id="comment1">... 

然後只要進入這個:

$('#comment' + id).slideDown(... 
0
$(".comment" + id).slideDown(180, function() { 

該ID將是直的文本,因此不會作爲選擇器工作,試試這個:

$(".comment #" + id).slideDown(180, function() { 
0

看看屬性選擇

改變這種

$(".comment" + id).slideDown(180, function() { 
    $('#container').isotope('reLayout'); 
}); 

$(".comment[id=" + id+"]").slideDown(180, function() { 
    $('#container').isotope('reLayout'); 
}); 
+0

是的!你真棒,它完美的作品! – 2012-03-26 15:55:33

+0

我試過所有其他的答案,這是唯一的工作。 – 2012-03-26 17:03:56

-1

我覺得應該是:

$(".comment#" + id).slideDown(180, function() { 
    $('#container').isotope('reLayout'); 
}); 

請注意選擇器中.comment之後的'#'。希望這可以幫助。

+0

呃 - 你爲什麼把這個答案投下來? - 這是正確的。如果您將鼠標懸停在分數下方的向下箭頭上,則會看到您在回答「無用」時投下。你是否總是咬牙切齒地試圖幫助你? – coalvilledave 2012-03-26 16:22:01

+0

特別是當你甚至沒有說明你的代碼不工作! – coalvilledave 2012-03-26 16:23:06

+0

我不知道是誰投了這個票,它不是我..謝謝你的迴應! – 2012-03-26 18:34:48

0

試試這個

$('.commentopen').click(function() { 
    var comment = $(this).attr('id'); 
    $('.comment[id="' + id + '"]').slideDown(180, function() { 
     $('#container').isotope('reLayout'); 
    }); 
}); 
+0

這沒有似乎工作:(將解析爲'$ – 2012-03-26 15:53:24

1

你永遠不應該有相同的ID多個元素。 ID應該始終是唯一的。

下面的工作:

<div class="commentopen" id="1">Comment</div> 
<div class="comment" id="c1"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="2">Comment</div> 
<div class="comment" id="c2"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="3">Comment</div> 
<div class="comment" id="c3"><textarea>write a comment...</textarea></div> 

和:

$('.commentopen').click(function() { 
    var comment = $(this).attr('id'); 
    $("#c" + id).slideDown(180, function() { 
     $('#container').isotope('reLayout'); 
    }); 
}); 

更新:我固定下面提到的錯誤。

+0

'$(「C」 + id)的''(」 CCO 1' ) - 如何的是,應該制定出 – Niko 2012-03-26 15:45:58

+0

難道不這樣輸出」 .cco1" 爲例如,我點擊第一個,有一個co1的id,然後它進入下滑功能「.c」+ co1 – 2012-03-26 15:51:33

+0

+1 b/c聲明有效 – nolabel 2012-03-26 16:15:53

-1

如果你有ID,你將不再需要在查詢與你的HTML

$('.commentopen').click(function() { 
    var commentId = $(this).attr('id'); 
    $("#"+ commentId).slideDown(180, function() { 
     $('#container').isotope('reLayout'); 
    }); 
}); 
+0

我想知道爲什麼-1票 – themarcuz 2012-03-26 15:50:21

+1

這隻會是有用的,如果.commentopen是div我試圖打開。謝謝你的迴應,雖然 – 2012-03-26 15:52:08

+0

'謝謝你的迴應,雖然'? - 是啊 - 不要指望有更多的'迴應',如果你投票的人失望,當你甚至不似乎知道什麼y你在做什麼。 – coalvilledave 2012-03-26 16:26:00

1

的一個問題是,你都在重複着標籤的ID不再使用的.comment類。該ID在HTML頁面上應該是唯一的。重複相同的ID導致一些JavaScript錯誤。

<div class="commentopen" id="1">Comment</div> 
<div class="comment" id="comment_1"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="comment_2">Comment</div> 
<div class="comment" id="comment_2"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="comment_3">Comment</div> 
<div class="comment" id="comment_3"><textarea>write a comment...</textarea></div> 

而JavaScript應該是這樣的

$('.commentopen').click(function() { 
    var comment = $(this).attr('id'); 
    $("#comment_" + id).slideDown(180, function() { 
     $('#container').isotope('reLayout'); 
    }); 
}); 
0

有與你在做什麼的幾個問題。首先,id屬性在頁面上必須是唯一的,因此它不能在2個div之間共享(例如comment和commentopen)。相反,你可以有一個基於另一個ID,如id="comment1"和id =「1」`。然後,你的頁面應該是這樣的:

<div class="commentopen" id="1">Comment</div> 
<div class="comment" id="comment1"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="2">Comment</div> 
<div class="comment" id="comment2"><textarea>write a comment...</textarea></div> 
<div class="commentopen" id="3">Comment</div> 
<div class="comment" id="comment3"><textarea>write a comment...</textarea></div> 

和jQuery代碼應該是這樣的:

$('.commentopen').click(function() { 
var comment = $(this).attr('id'); 
$("#comment" + id).slideDown(180, function() { 
     $('#container').isotope('reLayout'); 
    }); 
}); 
0

似乎具有了div相同的ID爲點擊textarea的的是造成問題。

看看所述的jsfiddle here

應該解決的問題

+0

啊這是一個很棒的代碼!非常感謝你! – 2012-03-26 18:37:22