jQuery selecting a selector with namespaced elemen

2020-01-31 06:05发布

How would I select the following in jQuery?

<ns:text value="my value" />

I have tried the snippets below, but to no avail. Is this possible?

var x= $('ns:text').attr('value'); 
return x;

var x= $('text').attr('value'); 
return x;

6条回答
做个烂人
2楼-- · 2020-01-31 06:49

Take a look at JQuery, XML and namespaces.

It seems that this should work:

var x = $("ns\\:text").attr("value");
查看更多
可以哭但决不认输i
3楼-- · 2020-01-31 06:57
$('ns\\\:text').attr('value')

seems to be the answer but some people have mentioned some browsers may not support it... but i don't know about that as this is a jquery library unless jquery has not implemented it then maybe that could be the case:

supported and tested on: ie8, FF 3.6, chrome 3.0 i have not tested it on other browsers because i don't have them but if anyone has the answer if it works on other browsers then please add on the comment below...

查看更多
霸刀☆藐视天下
4楼-- · 2020-01-31 06:59

You're looking for:

var x= $('ns\\:text').attr('value'); 
return x;

Ref:

查看更多
我想做一个坏孩纸
5楼-- · 2020-01-31 06:59

I tried the "[nodeName=gd:when]" version and it worked at first - but then when I upgraded jQuery from 1.4.3 to 1.5.2 it broke. Not sure which version inbetween is the problem.

Solution was to use "gd\:when" approach - but it didn't work in Google Chrome - so I finally came up with a way that works in all the browsers I tried:

// Need to work out how to handle namespaces on some elements.
var namespace = "gd\\:"; // Most prefer this...
var startDateElement = $(this).find( namespace + "when" );
if( startDateElement.length == 0 )
{
    namespace = ""; // ...but some don't!
    startDateElement = $(this).find( namespace + "when" );
}

You can then use the prefix for any more namespaced elements.

See http://www.kajabity.com/index.php/2011/05/handling-xml-elements-with-namespaces-in-jquery/.

查看更多
smile是对你的礼貌
6楼-- · 2020-01-31 07:05

Use a backslash, which itself should be escaped so JavaScript doesn't eat it:

   alert($('ns\\:text').attr('value') );

(as found on this thread)

查看更多
乱世女痞
7楼-- · 2020-01-31 07:05

Works in webkit and alle the other Browsers too


$('[nodeName=ns:text]').attr('value');

Example:

$('[nodeName=geo:lat]').attr('value');
$('[nodeName=geo:lat]').val();
查看更多
登录 后发表回答