我有一个标记插件( http://xoxco.com/projects/code/tagsinput/ ),唯一的问题是,我经常将内容加载到同一个 DOM 中 - 因此 .tagsInput()
再次被调用。问题是,当它多次发生时,插件将无法工作 - 所以首先我需要以某种方式解除它的绑定(bind),但没有内部方法......
最佳答案
您没有提供太多上下文,但如果您想绑定(bind)和取消绑定(bind),请查看 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
关于jQuery:tagsInput 重新初始化(或取消绑定(bind)所有内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17202922/