我有一个标记插件(http://xoxco.com/projects/code/tagsinput/)唯一的问题是,我经常将内容加载到同一个DOM中 - 所以.tagsInput()
再次调用。问题是,当它多次发生时,插件不会工作 - 所以首先我需要以某种方式解除绑定,但没有内部方法......
答案 0 :(得分:2)
你没有提供太多的上下文,但如果你想绑定和取消绑定,请在jQuery上看一下on()
和off()
:
<!DOCTYPE html>
<html>
<head>
<style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<button id="theone">Does nothing...</button>
<button id="bind">Add Click</button>
<button id="unbind">Remove Click</button>
<div style="display:none;">Click!</div>
<script>
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("body").on("click", "#theone", aClick)
.find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
$("body").off("click", "#theone", aClick)
.find("#theone").text("Does nothing...");
});
</script>
</body>
</html>
以下是demo
答案 1 :(得分:1)
我找到了一个解决方法,涉及编辑 jquery.tagsinput.js 文件。在200-202行附近有一个条件
if (!id || delimiter[$(this).attr('id')]) {
id = $(this).attr('id', 'tags' + new Date().getTime()).attr('id');
}
需要注释掉。它解决了“再生”问题。它是否会产生其他问题 - 我不知道,还不适合我。
我跟踪了变量 id 和分隔符[$(this).attr('id')] ,并发现即使将其设置为参数 - 启动函数第一次分隔符[$(this).attr('id')] 未定义(我想知道为什么,因为它是在参数中设置的) - 条件为false且 id 保持不变 - 给定id的名称。下次启动它 - 分隔符不再为空,id等于给定id的相同名称 - 条件为true,并且使用 getTime 生成id的新名称。这给出了问题 - 在代码中稍后无法知道正确的 html输入id 值。