添加所有标记<a> in a variable, except some with specific Id names</a>

时间:2014-09-17 05:45:54

标签: javascript jquery anchor

我有一个名为 notincluded 的变量:

var notincluded = “one,two,three”

让我们说由于某些原因我不能改变这个变量的形式。如何将“一,二,三”转换为可放入的内容:

var ids = $("a:not([href])").not ('notincluded');

所以最终我可以在 ids 中包含所有标记(锚点),但名为1,2和3的锚点除外。

很抱歉,如果我无法解释清楚,请告诉我是否可以添加更多详细信息以表达清楚,谢谢。

4 个答案:

答案 0 :(得分:2)

在字符串notincluded

之前添加#(id)前缀
var notincluded = "one,two,three";

notincluded  = '#'+notincluded.split(",").join(',#')

var ids = $("a:not([href])").not(notincluded);

答案 1 :(得分:2)

使用在每个ID前插入notincluded的regepx转换#的简便方法:

var notincluded = "one,two,three";
var ids = $("a:not([href])").not(notincluded.replace(/\b(?=\w)/g, '#'));

演示:http://jsfiddle.net/zvmLc7oc/

答案 2 :(得分:1)

在元素中添加一些类,不应将其包含在变量中。

例如,尝试这样:

var ids = $( "div" ).not( ".not" );

答案 3 :(得分:0)

1)您使用了错误的引号“ != "所以“one,two,three” != "one,two,three"

2)'notincluded'是一个字符串,而不是一个变量,所以'notincluded' != notincluded

3)我认为你想要的是:

                                 // build a selector of ids
var ids = $('a:not([href])').not('#'+ notincluded.split(',').join('#,'))