为什么我的跨度消失了?

时间:2010-10-14 15:46:42

标签: jquery jquery-dialog

我有一些像这样的HTML设置:

<div>
    <label for="amount"><a id="amount-help-icon" class="help icon-link" href="#"><span class="ui-icon ui-icon-help"></span></a> Amount:</label>
    <input id="amount" class="inputText" type="text" value="" maxlength="100" size="10" name="amount" />
    <span class="help">The amount for stuff</span>
</div>

我试图让jquery在点击帮助图标时显示一个对话框,所以我有这个:

$("a.help").click(function () {
    $(this).closest("label").siblings(".help").dialog({ title: "Help" });
    return false;
});

第一次显示对话框时效果很好,但是当我单击图标时,跨度会从DOM中消失。所以,如果我再次点击该图标,则没有任何反应(因为找不到span.help)。

2 个答案:

答案 0 :(得分:1)

您可能想克隆范围。

$(this).closest("label").siblings(".help").clone().dialog({ title: "Help" });

答案 1 :(得分:1)

以下内容允许您多次重复使用该对话框 - 但<span>会立即消失,而不是第一次点击。

$("a.help").each(function(i, link) {
    var $this = $(this), 
        d = $this.closest("label").siblings(".help").dialog({ title: "Help", autoOpen: false });
    $this.data('dialog', d);
});

$("a.help").click(function () {
    $(this).data('dialog').dialog('open');
    return false;
});​

演示here

相关问题