从jQuery标记名获取第一个元素

时间:2022-11-27 11:38:32

I am converting standard JavaScript over to jQuery for cross browser compatibility. I just want to know how will this be written in jQuery? In other words how do I find the first element?

为了跨浏览器兼容性,我将标准JavaScript转换为jQuery。我只是想知道如何用jQuery编写它?换句话说,我如何找到第一个元素?

var x = content.getElementsByTagName("title")[0].firstChild.data;

I have tried the following:

我试过以下方法:

var content = $.parseXML(zipEntry1.data);
content = $(content);
var x = content.find("title").first().val();

and

var content = $.parseXML(zipEntry1.data);
content = $(content);
var x = content.find("title")[0].val();

But none of those work. What would be the correct way?

但这些都不管用。正确的方法是什么?

EDIT:

编辑:

Here is some additional info. I am writing a Epub reader. The file I am parsing is content.opf within the Epub specification. Here is an extract:

这里有一些额外的信息。我正在写一本电子书。我正在解析的文件是内容。在Epub规范中的opf。这是一个精华:

<metadata>
<dc:rights>Public domain in the USA.</dc:rights>
<dc:identifier id="id" opf:scheme="URI">http://www.gutenberg.org/ebooks/6130</dc:identifier>
<dc:contributor opf:file-as="Buckley, Theodore Alois" opf:role="ann">Theodore Alois Buckley</dc:contributor>
<dc:creator opf:file-as="Homer">Homer</dc:creator>
<dc:contributor opf:file-as="Pope, Alexander" opf:role="trl">Alexander Pope</dc:contributor>
<dc:title>The Iliad</dc:title>
<dc:language xsi:type="dcterms:RFC4646">en</dc:language>
<dc:subject>Classical literature</dc:subject>
<dc:subject>Epic poetry, Greek -- Translations into English</dc:subject>
<dc:subject>Achilles (Greek mythology) -- Poetry</dc:subject>
<dc:subject>* War -- Poetry</dc:subject>
<dc:date opf:event="publication">2004-07-01</dc:date>
<dc:date opf:event="conversion">2012-10-31T19:41:56.338029+00:00</dc:date>
<dc:source>http://www.gutenberg.org/files/6130/6130-h/6130-h.html</dc:source>

I can get the author and title with this, but it only works in Chrome:

我可以用这个得到作者和标题,但它只能用Chrome:

content.find('creator:first').text();
content.find('title:first').text();

Version works in Firefox and Chrome:

版本适用于Firefox和Chrome:

content.find("package").attr("version");

And I have not yet got the publication date. This is what I tried:

我还没有得到出版日期。这就是我所尝试的:

 content.find('[event="publication"]').val();

3 个解决方案

#1


0  

If you need to get the text content of the first title use this:

如果您需要获得第一个标题的文本内容,请使用以下内容:

$('title:first', content).text();

However jQuery in Firefox require namespace to be specified as well:

但是Firefox中的jQuery也需要指定名称空间:

var ff = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
$((ff ? 'dc\\:' : '') + 'title:first', content).text();

Working example: http://jsbin.com/enayex/5/edit

工作示例:http://jsbin.com/enayex/5/edit

#2


0  

If you know the element type then this can be easy. Let us assume that we are dealing with divs, then do this:

如果您知道元素类型,那么这很容易。让我们假设我们正在处理divs,然后这样做:

$('div').first().html("<p>I found you</p>");

#3


0  

something like this maybe?

这样可能吗?

var x = content.find('title:first').html()

or

var x = content.find('title:first').text();

#1


0  

If you need to get the text content of the first title use this:

如果您需要获得第一个标题的文本内容,请使用以下内容:

$('title:first', content).text();

However jQuery in Firefox require namespace to be specified as well:

但是Firefox中的jQuery也需要指定名称空间:

var ff = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
$((ff ? 'dc\\:' : '') + 'title:first', content).text();

Working example: http://jsbin.com/enayex/5/edit

工作示例:http://jsbin.com/enayex/5/edit

#2


0  

If you know the element type then this can be easy. Let us assume that we are dealing with divs, then do this:

如果您知道元素类型,那么这很容易。让我们假设我们正在处理divs,然后这样做:

$('div').first().html("<p>I found you</p>");

#3


0  

something like this maybe?

这样可能吗?

var x = content.find('title:first').html()

or

var x = content.find('title:first').text();