我正在尝试使用由js生成的元素激活slideDown()onclick。

我从ryanfait.com下载了一个脚本,该脚本可让您自定义单选按钮的外观。这可以正常工作,但是当我尝试对生成的元素(在这种情况下为跨度)应用操作时,它将无法正常工作。我该如何解决?

这是我的HTML:

<div class="radiocont">
                                <label>Yes</label>
                                <input type="radio" class="styled" value="yes" name="watertank" />
                            </div>
                            <div class="radiocont">
                                <label>No</label>
                                <input type="radio" class="styled" value="no" name="watertank" />
                            </div>


DOM加载后,输入将替换为如下所示的跨度:
<span class="radio" title="yes" style="background-position: 0pt 0pt;"></span>

当您单击跨度之一时,我想激活slideDown()。这是我的js:$('[title="yes"]').click(function() { $(this).nextAll('.secondq:first').slideDown('slow');});

有人可以协助吗?

最佳答案

使用.live.delegate“现在和将来将与当前选择器匹配的所有元素的处理程序添加到事件”。

// .live()
$('[title="yes"]').live("click", function() {
    $(this).nextAll('.secondq:first').slideDown('slow');
});

// .delegate() (not entirely sure of your structure, the below may need
// to be modified accordingly
$('.radiocont').delegate("span[title='yes']", "click", function() {
    $(this).nextAll('.secondq:first').slideDown('slow');
});

关于javascript - 如何选择由jQuery生成的元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6214217/

10-12 00:04
查看更多