嗨我正在開發一個項目,點擊圖片後會生成關於點擊圖片的摘要。大約有50個圖像可以點擊。我想在列表中添加摘要,我想過濾它,所以如果我點擊圖像兩次,不會有多個附加。jQuery在列表中添加內容
下面是HTML
<div id="imageset">
<img id="001" class="swatch" src="1.jpg" title="swatch 1" bigimage="1b.jpg"/>
<img id="002" class="swatch" src="2.jpg" title="swatch 2" bigimage="2b.jpg"/>
<img id="003" class="swatch" src="3.jpg" title="swatch 3" bigimage="3b.jpg"/>
<img id="004" class="swatch" src="4.jpg" title="swatch 4" bigimage="4b.jpg"/>
....
</div>
<ul id="summary">
</ul>
下面的代碼是jQuery代碼
$("#001").click(function(){
this.t = this.title;
this.title = "";
this.b = this.bigimage;
this.bigimage = "";
$("ul#summary").append("<li><div id='t001'>"+this.t+"</div></br><img id='b001' src="+this.b+"/></li>");
});
$("#002").click(function(){
this.t = this.title;
this.title = "";
this.b = this.bigimage;
this.bigimage = "";
$("ul#summary").append("<li><div id='t002'>"+this.t+"</div></br><img id='b002' src="+this.b+"/></li>");
});
$("#003").click(function(){
this.t = this.title;
this.title = "";
this.b = this.bigimage;
this.bigimage = "";
$("ul#summary").append("<li><div id='t003'>"+this.t+"</div></br><img id='b003' src="+this.b+"/></li>");
});
........
,它的推移和對...有沒有一種方法來簡化這些代碼?如果是的話,我如何添加一個if和else語句來過濾掉內容是否已被添加?
非常感謝,這讓我的生活更輕鬆。林新編碼在jQuery中。您的代碼已經過簡化,並且認爲我如何驗證列表項目。 –
沒問題!很高興有幫助!祝你好運,並保持編碼! :) –