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"/>
甚至可能吗?您的代码存在问题:
返回一个节点列表对象,当您将其推送到数组并使用forEach时,只有1个循环,回调函数中的document.getElementsByClassName
对象是没有条目
方法的节点列表对象拆分
- 要访问隐藏字段值,您需要访问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
});
您的代码有问题:
返回一个节点列表对象,当您将其推送到数组并使用forEach时,只有1个循环,回调函数中的document.getElementsByClassName
对象是没有条目
方法的节点列表对象拆分
- 要访问隐藏字段值,您需要访问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
});