Javascript 纯js中的foreach循环,检查匹配项

Javascript 纯js中的foreach循环,检查匹配项,javascript,html,angularjs,Javascript,Html,Angularjs,我正在生成几个隐藏的输入字段,如: <input class="activeList" type="hidden" value="foobar-value"/> 甚至可能吗?您的代码存在问题: document.getElementsByClassName返回一个节点列表对象,当您将其推送到数组并使用forEach时,只有1个循环,回调函数中的条目对象是没有拆分方法的节点列表对象 要访问隐藏字段值,您需要访问DOM的value属性 使用split('-')而不是split(','

我正在生成几个隐藏的输入字段,如:

<input class="activeList" type="hidden" value="foobar-value"/>

甚至可能吗?

您的代码存在问题:

  • document.getElementsByClassName
    返回一个节点列表对象,当您将其推送到数组并使用forEach时,只有1个循环,回调函数中的
    条目
    对象是没有
    拆分
    方法的节点列表对象
  • 要访问隐藏字段值,您需要访问DOM的
    value
    属性
  • 使用
    split('-')
    而不是
    split(',')
尝试:

另一种解决方案是使用
Array.prototype.forEach.call

var activeList = document.getElementsByClassName('activeList');

Array.prototype.forEach.call(activeList ,function(entry){    
   //Your code just like above
});

您的代码有问题:

  • document.getElementsByClassName
    返回一个节点列表对象,当您将其推送到数组并使用forEach时,只有1个循环,回调函数中的
    条目
    对象是没有
    拆分
    方法的节点列表对象
  • 要访问隐藏字段值,您需要访问DOM的
    value
    属性
  • 使用
    split('-')
    而不是
    split(',')
尝试:

另一种解决方案是使用
Array.prototype.forEach.call

var activeList = document.getElementsByClassName('activeList');

Array.prototype.forEach.call(activeList ,function(entry){    
   //Your code just like above
});

您应该使用
ng model
访问输入值。您添加了对pure.js的引用吗?您应该使用
ng model
访问输入值。您添加了对pure.js的引用吗?非常有用,谢谢。但是我还想删除jQuery的内容,因为我无法访问jQuery-这是一个角度项目。编辑:
TypeError:Object#没有两种方法“forEach”
。@user3233979:关于删除jQuery,没有必要,我们可以同时使用jQuery和Angular,谢谢。但我还想删除jQuery的内容,因为我无法访问jQuery-这是一个角度项目。编辑:
TypeError:Object#对这两个都没有方法“forEach”
。@user3233979:关于删除jQuery,没有必要,我们可以同时使用jQuery和角度项目
var activeList = document.getElementsByClassName('activeList');//document.getElementsByClassName returns a NodeList

for (i=0;i<activeList.length;i++)
{    
    var string = activeList[i].value.split('-'); // you have to access the value attribute and change , to -

    $('.'+string[0]).each(function()
    {
        if($(this).text() == string[1])
        {
            $(this).addClass('yes'); 
            // makes: .foobar.yes
        }
    });

    if (document.getElementsByClassName(string[0]).length){ 
        /// this is the farest i get.   
    }
};
var activeList = Array.prototype.slice.call(document.getElementsByClassName('activeList'));

activeList.forEach(function(entry)
    {    
        var string = entry.value.split('-'); // you have to access the value attribute and change , to -

        $('.'+string[0]).each(function()
        {
            if($(this).text() == string[1])
            {
                $(this).addClass('yes'); 
                // makes: .foobar.yes
            }
        });

        if (document.getElementsByClassName(string[0]).length){ 
            /// this is the farest i get.   
        }
  });
var activeList = document.getElementsByClassName('activeList');

Array.prototype.forEach.call(activeList ,function(entry){    
   //Your code just like above
});