使引导标记响应,jquery 事件丢失
Making bootstrap-tags responsive, jquery events lost
我正在尝试更改此演示:http://maxwells.github.io/bootstrap-tags.html
变成一个响应式版本,我可以在其中将其设置为只读并根据需要将其从只读中删除。此代码:
var alltags = ["new tag", "testtag", "tets", "wawa", "wtf", "wtf2"];
$(document).ready(function() {
var tagbox = $('#my-tag-list').tags({
suggestions: alltags
});
var tagenable = true;
$('#my-tag-list').focusout(function() {
if (tagenable) {
tagbox.readOnly = true;
$('#my-tag-list').empty();
tagbox.init();
tagenable = false;
}
});
$('#my-tag-list').click(function() {
if(!tagenable) {
tagbox.readOnly = false;
$('#my-tag-list').empty();
tagbox.init();
tagenable = true;
}
});
});
似乎工作得很好,它使所有内容在聚焦后只读,并且在我单击它时可编辑。但是,编辑不起作用,因为我无法插入新标签或删除它们(似乎事件处理丢失或类似的东西)。
我猜清空 #my-tag-listdiv 会导致这种情况,但我还找不到一种方法来使用例如"分离"来删除内部的所有内容(而不是元素本身)并将其放回原处。
我试图制作一个JS小提琴,但它还没有真正工作得那么好:http://jsfiddle.net/tomzooi/cLxz0L06/确实有效的事情是保存整个网站,如下所示:https://www.dropbox.com/sh/ldbfqjol3pppu2k/AABhuJA4A6j9XTxUKBEzoH6za?dl=0此链接具有我正在使用的引导标签内容的未最小化JS:https://github.com/maxwells/bootstrap-tags/blob/master/dist/js/bootstrap-tags.js
到目前为止,
我设法通过对引导JavaScript代码的一些修改来做到这一点。我使用两个不同的标签框,我通过一些点击事件隐藏和取消隐藏。
var tagbox = $('#my-tag-list').tags({
suggestions: alltags,
tagData: tmp_tags,
afterAddingTag: function(tag) { tagboxro.addTag(tag); },
afterDeletingTag: function(tag) {tagboxro.removeTag(tag); }
});
var tagboxro = $('#my-tag-listro').tags({
suggestions: alltags,
tagData: tmp_tags,
readOnly: 'true',
tagSize: 'sm',
tagClass: 'btn-info pull-right'
});
$(document).mouseup(function (e) {
var container = $("#my-tag-list");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) { // ... nor a descendant of the container
if (tagsave) {
$("#my-tag-listro").show();
$("#my-tag-list").hide();
var tags = tagbox.getTags();
$.post("%basedir%/save.php", {
editorID:"new_tags",
tags:tags
}, function(data,status){
//alert("Data: " + data + "'nStatus: " + status);
});
tagsave = false;
}
}
});
$('#my-tag-listro').click(function() {
tagsave = true;
//$(".tag-list").toggle();
$("#my-tag-list").show();
$("#my-tag-listro").hide();
});
我不得不修改引导标签的代码.js以允许这样做,因为它通常会删除所有有用的函数,当它在 init 函数中被视为只读时:
if (this.readOnly) {
this.renderReadOnly();
this.removeTag = function(tag) {
if (_this.tagsArray.indexOf(tag) > -1) {
_this.tagsArray.splice(_this.tagsArray.indexOf(tag), 1);
_this.renderReadOnly();
}
return _this;
};
this.removeTagClicked = function() {};
this.removeLastTag = function() {};
this.addTag = function(tag) {
_this.tagsArray.push(tag);
_this.renderReadOnly();
return _this;
};
this.addTagWithContent = function() {};
this.renameTag = function() {};
return this.setPopover = function() {};
}
如果以不那么黑客的方式合并此功能,那就太棒了:)
相关文章:
- 在创建对象后附加一个jquery事件
- 将jQuery事件应用于所有类元素
- Jquery事件处理程序仅适用于匿名函数
- 如何在jQuery事件处理程序中存储和重用超时
- 无线电输入更改的jQuery事件未启动
- 我应该/如何清除mousemove JQuery事件侦听器
- 是否可以在jQuery事件中更改Angular范围
- jQuery事件侦听器多次启动
- jQuery 事件处理程序堆叠
- 如何在不删除类似侦听器的情况下从父对象中删除jQuery事件侦听器
- 一个接一个地触发jQuery事件
- 在JQuery事件中查找父元素
- 堆叠jQuery事件(动画)
- 在jquery事件中引用javascript对象
- 动态添加对象的jQuery事件处理程序
- jQuery事件命名空间是否可以包含破折号
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- 没有为Ajax添加的新元素注册Jquery事件
- 动态创建的DOM元素上的jQuery事件绑定
- jQuery事件未在动态生成的内容上激发