如果选项卡已经存在,如何防止插入该选项卡
How to prevent the insertion of a tab if one already exists?
我的问题很简单,我所需要做的就是在已经存在的情况下阻止插入选项卡
问题:我有一个搜索栏,当用户在其中输入名称时,结果被包装在一个类为.result_container_2
的div中,现在当用户单击该选项卡时,一个选项卡被插入到另一个类别为.selected_recipients
的div中。现在我想让脚本检测标签是否已经插入,我已经用.each()
尝试过了,但没有成功
脚本:下面是生成选项卡的当前脚本
$(document).on('click', ".result_container_2", function() {
var to_disp = $(this).data('recipient_disp');
var to_show = $(this).data('recipient_show');
var rel = $(this).attr('rel'); //the current account
if (to_disp != rel) { //if the clicked tab is not the current account
var a = $(".selected_recipients").children().length;
var b = a++;
$(".selected_recipients").show().append('<span class="recipient_tab" id="' + b + '" data-recipient="' + to_disp + '"><span style="display:block;float:left;">' + to_show + '</span><span class="recipient_remove">x</span></span>');
$(".display_found_query_cont_mess_drawer").hide();
}
});
//---for removing recipient----
$(document).on('click', ".recipient_remove", function() {
$(this).parent(".recipient_tab").remove();
if ($(".selected_recipients").children().length == 0) {
$(".selected_recipients").hide();
}
})
/*-------recipients tabs -------*/
.selected_recipients {
display: inline-block;
padding: 5px 0px;
border-bottom: 1px solid #CCC;
width: 100%;
}
.recipient_tab {
float: left;
display: block;
padding: 4px;
margin-left: 5px;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2;
background-image: -moz-linear-gradient(#777, #666);
background-image: -webkit-gradient(linear, left top, left bottom, from(#777), to(#666));
background-image: -webkit-linear-gradient(#777, #666);
background-image: -o-linear-gradient(#777, #666);
background-image: -ms-linear-gradient(#777, #666);
background-image: linear-gradient(#777, #666);
}
.recipient_remove {
padding: 2px;
float: left;
display: block;
margin-left: 5px;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2;
background-color: #C0C0C0;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selected_recipients"></div>
<div class="result_container_2" rel="test" data-recipient_disp="mark" data-recipient_show="mark">mark</div>
<div class="result_container_2" rel="test" data-recipient_disp="mark1" data-recipient_show="mark1">mark1</div>
<div class="result_container_2" rel="test" data-recipient_disp="test" data-recipient_show="test">test **the tab with this name wont be created**</div>
我该如何完成
试试这个:演示
$(document).ready(function(){
$(document).on('click', ".result_container_2", function() {
var to_disp = $(this).data('recipient_disp');
var to_show = $.trim($(this).data('recipient_show'));
var rel = $(this).attr('rel');
if (to_disp != rel && !$('.selected_recipients .recipient_tab[data-recipient="'+to_show+'"]').length)
{
var a = $(".selected_recipients").children().length;
var b = a++;
$(".selected_recipients").show().append('<span class="recipient_tab" id="' + b + '" data-recipient="' + to_disp + '"><span style="display:block;float:left;">' + to_show + '</span><span class="recipient_remove">x</span></span>');
$(".display_found_query_cont_mess_drawer").hide();
}
});
$(document).on('click', ".recipient_remove", function() {
$(this).parent(".recipient_tab").remove();
if ($(".selected_recipients").children().length == 0){
$(".selected_recipients").hide();
}
})
});
查看这是否适用于
if (to_disp != rel) { //if the clicked tab is not the current account
var a = $(".selected_recipients").children().length;
var b = a++;
var present=false;
var i=0;
while(i<b)
{
if($('#'+i).data('recipient') == to_disp)
present=true;
i++;
}
if(present)
return;
$(".selected_recipients").show().append('<span class="recipient_tab" id="' + b + '" data-recipient="' + to_disp + '"><span style="display:block;float:left;">' + to_show + '</span><span class="recipient_remove">x</span></span>');
$(".display_found_query_cont_mess_drawer").hide();
}
如果我们找到匹配集exists=true;
,您可以通过迭代现有选项卡并检查to_show
是否存在来实现这一点
exists = false;
$.each($('.recipient_tab'),function(index,element){
if($(element).attr('data-recipient') == to_disp){
exists = true;
}
})
if(!exists){
$(".selected_recipients").show().append('<span class="recipient_tab" id="' + b + '" data-recipient="' + to_disp + '"><span style="display:block;float:left;">' + to_show + '</span><span class="recipient_remove">x</span></span>');
}
相关文章:
- 如何防止onclick事件在select选项中触发
- 如何防止iframe中的链接在新选项卡中打开
- 如何防止更改某些小部件's选项
- 如何防止用户使用浏览器选项禁用脚本
- 防止已选择的选项 Jquery 使用 MYSQL 自动完成
- 如何防止在单击时选择选项卡
- 如何防止页面和选项卡的文档.title 更改
- 如何在选项卡或浏览器以角度关闭时防止Default()
- 防止Jquery选项卡中的dafail操作
- 若在两个不同的选项卡中打开同一个页面,如何防止javascript运行两次
- 如何防止引导选项卡干扰angularjs路由
- jquery插件选项数组,防止数组覆盖
- 防止具有相同选项的两个复选框上的重复选项
- 如何防止 ASP.NET 下拉列表在选择ON选项时触发其onclick事件
- 如何使用易选项卡jQuery插件防止在主页中跳转/滚动命名锚点
- (jQuery Tab UI) 如何防止无限制的 jQuery 选项卡创建
- 防止为每个表行选择多个相同值的选项
- 如何防止 JQuery 中的默认选项卡切换功能
- 防止在引导选项卡中加载动态连续网络两次
- 如何防止在提交表单后恢复到上一个选项卡