0

for some reason the following is not working. What's wrong?

I'm just trying to toggle visibility of element. I have multiple links with class 'variant_description_link' on the page since I add form through js as well. All I want is find the closest 'variant-description' block and toggle visibility.

$('.variant_description_link').click(function() {
  $(this).closest('.variant-description').toggle();
});
4

2 回答 2

1

我猜这些元素在 DOM 中实际可用之前就被 jQuery 调用了。

$(document).ready(function() {
    $('.variant_description_link').click(function() {
        $(this).toggle();
    });
});

(工作示例)

于 2013-03-23T18:13:03.663 回答
0

由于我也通过 js 添加表单,因此我在页面上有多个带有“variant_description_link”类的链接。

听起来您需要使用on委托事件进行点击..如果您通过 js 添加的表单包含variant_description_link

尝试这个

 $(document).on('click','.variant_description_link',function() {
   $(this).closest('.variant-description').toggle();
});
于 2013-03-23T17:59:37.460 回答