![](/img/trans.png)
[英]Adding an onclick event to a new html element only working on the last element
[英]adding a jQuery effect only to the HTML element parent onclick
我想將jQuery效果“ flash”應用於html元素“ item_add”的父級“ simpleCart_shelfItem”。 這是為了表明用戶已將商品添加到購物車。 我通過引用父對象來完成此操作,因為有許多“ simpleCart_shelfItem”實例,並且我希望效果僅在被單擊的對象上發生。 我已經閱讀了this
,從我收集的,我不能擁有onClick
事件內聯,因為this
將是全球性的,而不是指元素的功能。 這就是我所擁有的:
Java腳本
$(".item_add").onclick = highlight;
function highlight(){
$(".item_add").parent().effect("highlight", {}, 750);
};
HTML
<div class="simpleCart_shelfItem">
<div class="image"><img src="css/graphics/jpeg/saw.jpg" alt="Hacksaw" height="75" width="75" /></div>
<h2 class="item_name">Hacksaw</h2>
<span class="item"><img src="css/graphics/jpeg/cart.jpg" alt="Shopping Cart" height="20" width="19" />
Items in Cart: <b class="simpleCart_quantity"></b></span>
<div class="description"><p>Quality metal hacksaw and 24 tooth blade.</p><p class="p_height">Your going to need some way of cutting that pipe. Good for the toolbox.</p></div>
<a class="add_products" href="cart.html">Go to Cart</a>
<a class="add_products" href="products.html" title="Add Items">Add More Items</a>
<span class="gst">(Inc. GST)</span>
<span class="item_price">$24.95</span>
<input class="item_quantity" value="1" type="text">
**<a href="javascript:;" class="item_add" onclick="highlight();">Add</a>**
</div><!--end of simpleCart_shelfItem-->
$(".item_add").click(function() {
$(this).parent().effect("highlight", {}, 750);
});
在上面的示例中, this
是指單擊的元素。 我不知道你到底的意思是this
是全球性的,但你錯了:-)(或我誤解你了)。
從http://www.quirksmode.org/js/this.html :
在JavaScript中,它總是指我們正在執行的函數的“所有者”,或更確切地說,指的是函數作為方法的對象。
查看演示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.