Javascript 使用委托获取数据属性
这对我来说非常有用,我能够Javascript 使用委托获取数据属性,javascript,jquery,delegation,Javascript,Jquery,Delegation,这对我来说非常有用,我能够获得数据俱乐部id: <!-- this is an example --> <a class="clubCode" href="" data-club-id= "1234">join with the code</a> $("a.clubCode").on("click",function(e){ e.preventDefault(); var clubId = $(this).data('club
获得数据俱乐部id
:
<!-- this is an example -->
<a class="clubCode" href="" data-club-id= "1234">join with the code</a>
$("a.clubCode").on("click",function(e){
e.preventDefault();
var clubId = $(this).data('club-id');
alert(clubId)
});
我怎么做 这样试试
$(".eventFeedbackBottomContainer").on("click", "a.editReview", function(e){
e.preventDefault();
var reviewId = $(this).attr('data-review-id'); //use .attr() for accessing attibutes
alert(reviewId);
});
html 5之前页面的实时演示:
html 5页中的实时演示:
快乐编码:)
我只能认为“editReview”类的元素不是“eventFeedbackBottomContainer”的子元素。除此之外,你写的这本书似乎还管用
...
修复了它。问题是
在
内。我一把它搬出去,一切都好了
这里的我在你的markupsorry中没有看到一个
eventFeedbackBottomContainer
,我认为这不是我问题的重点,我没有发布我认为不相关的内容。我现在将添加它。@RGraham请查看我编辑的问题。奇怪。您是否有可能在一个自包含的示例中复制生成的标记,正如它在这里所显示的那样:它也适用于我:“eventFeedbackBottomContainer”中是否包含“editReview”?。data
仍然返回HTML5数据属性值,所以在这个例子中,这两个可能是等价的。@RGraham:哦,因为OP没有提到html5或html4页面,所以我没有注意到这一点。我还添加了一个html5风格的代码。使用数据-
意味着html5,但即使是一个标有@RGraham的页面:是的,我想我只是忽略了操作代码。当你在jquery中投入太多的时候,你往往会忘记一些基本的东西:P。无论如何,感谢你注意到了这一点。起初我自己也这么想,但OP说,reviewId返回undefined
,表示警报确实执行了,但是。data
没有执行return@RGraham:我也无法复制“未定义的”。我想是纯粹的猜测吧。
<div class="eventFeedbackBottomContainer">
<div class="tabs">
<ul>
<li><a href="#panel1" tabindex="1">Reviews</a></li>
<li><a href="#panel2" tabindex="2">Hotels</a></li>
</ul>
</div>
<div class="rightFeedbackBottomContainer">
<a href="/addReview/<?php echo escape($eventReview->getData()->race_id);?>/" id="" class="Smallbut">Add Review</a>
</div>
<div id="panel1" class="panel">
<div class="feedbacksContainer">
<div class="show-reviews"></div><!-- a.editReview is inside each review loaded from a javascript file in this div-->
</div>
</div>
<div id="panel2" class="panel"/>
</div>
$(".eventFeedbackBottomContainer").on("click", "a.editReview", function(e){
e.preventDefault();
var reviewId = $(this).attr('data-review-id'); //use .attr() for accessing attibutes
alert(reviewId);
});
<div class="eventFeedbackBottomContainer">
...
<a class="editReview" data-review-id="123" href="">Edit</a>
<a class="editReview" data-review-id="456" href="">Edit</a>
</div>