Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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之类的选择器_Javascript_Jquery_Regex_Phantomjs_Casperjs - Fatal编程技术网

Javascript 在自定义对象上使用诸如jQuery之类的选择器

Javascript 在自定义对象上使用诸如jQuery之类的选择器,javascript,jquery,regex,phantomjs,casperjs,Javascript,Jquery,Regex,Phantomjs,Casperjs,我正在编写一些casperjs脚本,在那里我有一个如下所示的对象数组: [ { "element": { "attributes": { "href": "http://google.com", "rel": "google" }, "html": "link1", "nodeName": "a",

我正在编写一些casperjs脚本,在那里我有一个如下所示的对象数组:

[
    {
        "element": {
            "attributes": {
                "href": "http://google.com",
                "rel": "google"
            },
            "html": "link1",
            "nodeName": "a",
            "tag": "<a href=\"http://google.com\" rel=\"google\">link1</a>",
            "text": "link1"
        }
    },
    {
        "element": {
            "attributes": {
                "class": "someclass",
                "href": "http://yahoo.com",
                "id": "yahooid"
            },
            "html": "yahoo anchor",
            "nodeName": "a",
            "tag": "<a href=\"http://yahoo.com\" id=\"yahooid\" class=\"someclass\">yahoo anchor</a>",
            "text": "yahoo anchor
        }
    }
]
[
{
“要素”:{
“属性”:{
“href”:”http://google.com",
“rel”:“谷歌”
},
“html”:“link1”,
“nodeName”:“a”,
“标记”:“,
“文本”:“链接1”
}
},
{
“要素”:{
“属性”:{
“类”:“someclass”,
“href”:”http://yahoo.com",
“id”:“yahooid”
},
“html”:“yahoo anchor”,
“nodeName”:“a”,
“标记”:“,
“文本”:“雅虎锚”
}
}
]
对于我需要使用这些链接完成的一些任务,如果我可以使用类似jQuery的选择器来选择它们,那将非常有用

我有点想知道如何编写这样的代码,但是为所有类型的选择器([name |=“value”]、[name*=“value”]、[name*=“value”]、[name~=“value”]、[name$=“value”]等)编写所有正则表达式会让我做噩梦


我能做到这一点的最简单方法是什么?

如果您的要求是只从对象数组中获取锚定标记链接,那么这可能会有所帮助。还要注意,在您的OP中,最后一个元素有一个错误
“text”:“yahoo anchor
缺少一个结束
。希望这是您的输入错误

var objArray=[
{
“要素”:{
“属性”:{
“href”:”http://google.com",
“rel”:“谷歌”
},
“html”:“link1”,
“nodeName”:“a”,
“标记”:“,
“文本”:“链接1”
}
},
{
“要素”:{
“属性”:{
“类”:“someclass”,
“href”:”http://yahoo.com",
“id”:“yahooid”
},
“html”:“yahoo anchor”,
“nodeName”:“a”,
“标记”:“,
“文本”:“雅虎锚”
}
}
]
$(函数(){
$.each(objArray,function(i,v){
var link=this.element.attributes.href;
$('div').append(link+'
'); }); });

这就是我最终使用的:

function select( selector ) {
    var whitespace = "[\\x20\\t\\r\\n\\f]",
    identifier = "(?:\\\\.|[\\w-]|[^\0-\\xa0])+",
    attributes = "\\[" + whitespace + "*(" + identifier + ")(?:" + whitespace + "*([*^$|!~]?=)" + whitespace + "*(?:\"((?:\\\\.|[^\\\\\"])*)\"))" + whitespace + "*\\]",
    elFound = [];

    try {
        links.forEach( function(link) {
            expr = new RegExp( attributes );
            match = expr.exec( selector );
            attribute = match[1];
            operator = match[2];
            value = match[3];
            check = ( attribute == 'text' ) ? link.element[attribute] : link.element.attributes[attribute];

            switch( operator ) {
                    case "=":
                          if( check && check == value ) { elFound.push( link ); }
                          break;
                    case "!=":
                          if( check && check != value ) { elFound.push( link ); }
                          break;
                    case "*=":
                          if( check && check.indexOf( value ) > -1 ) { elFound.push( link ); }
                          break;
                    case "^=":
                          if( check && check.indexOf( value ) === 0 ) { elFound.push( link ); }
                          break;
                    case "$=":
                          if( check && check.slice( -value.length ) == value ) { elFound.push( link ); }
                          break;
                    default:
                          break;
            }
        });
    } catch(e) {
        console.log(e);
    }

    return elFound;
}

创建一个
div
,根据您拥有的元素列表添加标记…使用
jquery
您只需要锚定标记链接??来自此对象数组?您的对象数组看起来像是您做了一些奇怪事情的Dom元素。