Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery将子innerText与用户输入匹配_Javascript_Jquery_Dom - Fatal编程技术网

Javascript 如何使用jQuery将子innerText与用户输入匹配

Javascript 如何使用jQuery将子innerText与用户输入匹配,javascript,jquery,dom,Javascript,Jquery,Dom,我的结构如下: <div id="campaignTags"> <div class="tags">Tag 1</div> <div class="tags">Tag 2</div> <div class="tags">Tag 3</div> </div> 变量值仅用于演示目的 再多了解一下上下文: 每个div.tags都会动态添加到div#activitytags中,但我希

我的结构如下:

<div id="campaignTags">
    <div class="tags">Tag 1</div>
    <div class="tags">Tag 2</div>
    <div class="tags">Tag 3</div>
</div>
变量值仅用于演示目的

再多了解一下上下文:

每个
div.tags
都会动态添加到
div#activitytags
中,但我希望避免重复值。换句话说,如果用户再次尝试插入“标记1”,该函数将退出

任何指向正确方向的帮助都将不胜感激

编辑

这是我刚创作的小提琴:

与此问题相关的行是153-155

我尝试了所有的解决方案,但是仍然插入了标记,我想这是因为
return
语句只是返回了最新的函数和包装函数

有办法解决这个问题吗

这个怎么样:

var $taggedChild = $('#campaignTags').children().filter(function() {
  return $(this).text() === value;
});
下面是一个例子,说明了这种方法的实际应用:


但我可能会在这里使用另一种方法,将标记存储在JS本身中,并在必要时更新此哈希。大概是这样的:

var $container = $('#campaignTags'),
    $template  = $('<div class="tags">'),
    tagsUsed   = {};
$.each($container.children(), function(_, el) {
    tagsUsed[el.innerText || el.textContent] = true;
});

$('#tag').keyup(function(e) {
    if (e.which === 13) {
        var tag = $.trim(this.value);
        if (! tagsUsed[tag]) {
            $template.clone().text(tag).appendTo($container);
            tagsUsed[tag] = true;
        }
    }
});
var$container=$(“#活动标签”),
$template=$(“”),
tagsUsed={};
$.each($container.children(),function(\uel,el){
tagsUsed[el.innerText | | el.textContent]=true;
});
$('#tag').keyup(函数(e){
如果(e.which==13){
var标记=$.trim(此.value);
如果(!tagsUsed[tag]){
$template.clone().text(tag).appendTo($container);
tagsUsed[tag]=true;
}
}
});
我在这里使用了
$.trim
对值进行预处理,以防止添加诸如
'Tag 3'
'Tag 3'
等标记。通过直接比较(
==
),它们将通过

.

这个怎么样:

var $taggedChild = $('#campaignTags').children().filter(function() {
  return $(this).text() === value;
});
下面是一个例子,说明了这种方法的实际应用:


但我可能会在这里使用另一种方法,将标记存储在JS本身中,并在必要时更新此哈希。大概是这样的:

var $container = $('#campaignTags'),
    $template  = $('<div class="tags">'),
    tagsUsed   = {};
$.each($container.children(), function(_, el) {
    tagsUsed[el.innerText || el.textContent] = true;
});

$('#tag').keyup(function(e) {
    if (e.which === 13) {
        var tag = $.trim(this.value);
        if (! tagsUsed[tag]) {
            $template.clone().text(tag).appendTo($container);
            tagsUsed[tag] = true;
        }
    }
});
var$container=$(“#活动标签”),
$template=$(“”),
tagsUsed={};
$.each($container.children(),function(\uel,el){
tagsUsed[el.innerText | | el.textContent]=true;
});
$('#tag').keyup(函数(e){
如果(e.which==13){
var标记=$.trim(此.value);
如果(!tagsUsed[tag]){
$template.clone().text(tag).appendTo($container);
tagsUsed[tag]=true;
}
}
});
我在这里使用了
$.trim
对值进行预处理,以防止添加诸如
'Tag 3'
'Tag 3'
等标记。通过直接比较(
==
),它们将通过


.

您可以使用.innerHTML.text()

if(value === $this.text()){   // jQuery
    return;
}

您可以使用.innerHTML.text()

if(value === $this.text()){   // jQuery
    return;
}

不确定是否是打字错误,但您缺少一个接近的
}
。也许可以使用jquery
.text()
方法而不是
innerText

var value = "Tag 1";
$('#campaignTags').find(".tags").each(function(){
  var content = $(this).text();
  if(value === content){
    return;
  }
})

不确定是否是打字错误,但您缺少一个接近的
}
。也许可以使用jquery
.text()
方法而不是
innerText

var value = "Tag 1";
$('#campaignTags').find(".tags").each(function(){
  var content = $(this).text();
  if(value === content){
    return;
  }
})
我建议:

$('#addTag').keyup(function (e) {
    if (e.which === 13) {
        var v = this.value,
            exists = $('#campaignTags').children().filter(function () {
                return $(this).text() === v;
            }).length;
        if (!exists) {
            $('<div />', {
                'class': 'tags',
                'text': v
            }).appendTo('#campaignTags');
        }
    }
});
$('#addTag').keyup(函数(e){
如果(e.which==13){
var v=该值,
exists=$('#活动标签').children().filter(函数(){
返回$(this).text()==v;
}).长度;
如果(!存在){
$('', {
“类”:“标记”,
“文本”:v
}).appendTo(“#活动标签”);
}
}
});

这显然是基于一些假设:

  • 要添加唯一的新标记
  • 您希望用户在
    输入中输入新标记,然后按enter键添加
  • 参考资料:

      • 我建议:

        $('#addTag').keyup(function (e) {
            if (e.which === 13) {
                var v = this.value,
                    exists = $('#campaignTags').children().filter(function () {
                        return $(this).text() === v;
                    }).length;
                if (!exists) {
                    $('<div />', {
                        'class': 'tags',
                        'text': v
                    }).appendTo('#campaignTags');
                }
            }
        });
        
        $('#addTag').keyup(函数(e){
        如果(e.which==13){
        var v=该值,
        exists=$('#活动标签').children().filter(函数(){
        返回$(this).text()==v;
        }).长度;
        如果(!存在){
        $('', {
        “类”:“标记”,
        “文本”:v
        }).appendTo(“#活动标签”);
        }
        }
        });
        

        这显然是基于一些假设:

      • 要添加唯一的新标记
      • 您希望用户在
        输入中输入新标记,然后按enter键添加
      • 参考资料:


          • 给你试试这个:演示

            因为上面的大部分文章都有一些内容,所以这里是对解决方案的另一种看法。
            :)

            同样来自我以前的回答:

            希望它符合“:)”的需要,并在您的主要定制Jquery库中添加justText函数

            代码

             jQuery.fn.justtext = function () {
            
                 return $(this).clone()
                     .children()
                     .remove()
                     .end()
                     .text();
            
             };
            
             $(document).ready(function () {
                 var value = "Tag 1";
                 $('#campaignTags').children().each(function () {
                     var $this = $(this);
                     if (value == $(this).justtext()) {
                         alert('Yep yo, return');)
                         return;
                     }
                 });
            
                 //
             });
            

            试试这个:Demo

            因为上面的大部分文章都有一些内容,所以这里是对解决方案的另一种看法。
            :)

            同样来自我以前的回答:

            希望它符合“:)”的需要,并在您的主要定制Jquery库中添加justText函数

            代码

             jQuery.fn.justtext = function () {
            
                 return $(this).clone()
                     .children()
                     .remove()
                     .end()
                     .text();
            
             };
            
             $(document).ready(function () {
                 var value = "Tag 1";
                 $('#campaignTags').children().each(function () {
                     var $this = $(this);
                     if (value == $(this).justtext()) {
                         alert('Yep yo, return');)
                         return;
                     }
                 });
            
                 //
             });
            

            你为什么要重新发明jQuery的
            text()
            ,可能是因为在过去的11个小时里我一直在symfony和javascript之间切换,哈哈!)你为什么要重新发明jQuery的
            text()
            ,可能是因为在过去的11个小时里我一直在symfony和javascript之间切换,哈哈!)然后检查将是
            if($taggedChild.length>0){return;}
            对吗?筛选器是否仍返回jQuery对象数组?我想它会的。它返回一个jQuery对象,对吧,所以你应该检查它的
            长度
            。我认为这是最好的答案。忘记了
            过滤器(filter
            :)这是一个非常好的过滤器,不幸的是,当我在其余部分使用您的代码片段时