English 中文(简体)
jQuery not creating area tags
原标题:

I m trying to use jQuery to dynamically create an image map, and I ran into a weird behavior:

alert( $( <area /> ).size() );                         // 1
alert( $( <area shape="circle" /> ).size() );          // 0
alert( $( <area /> ).attr("shape", "circle").size() ); // 1

In other words, I can t create an area tag all at once; if I say

$( <area shape="circle" coords="50,25,4" href="#" alt="foo" /> )

then nothing is created. However, it works if I do it gradually, e.g.

$( <area /> )
    .attr("shape", "circle")
    .attr("coords", "50,25,4")
    .attr("href", "#")
    .attr("alt", "foo");

I have no idea why this is, because I can create all kinds of other elements with attributes and content, e.g.

$( <div id="foo" /> )
$( <div id="bar">Hello World!</div> )

so I m not clear on why this isn t working. This isn t all that important since I can make it wirk by chaining calls to attr, but that s annoying and I d like to understand this behavior.

最佳回答

An <area /> element is only defined inside an image map (i.e. <map> element). So essentially the following is failing (as this is what jQuery is doing with your markup):

var div = document.createElement( div );
div.innerHTML =  <area shape="circle" coords="50,25,4" href="#" alt="foo" /> ;
return div.childNodes; // this is empty, as the browser didn t allow 
                       // the <area /> element inside the <div> element

It s just one of those things obviously the great jQuery has not accounted for (yet). In the meantime try:

var $area = $(
     <map><area shape="circle" coords="50,25,4" href="#" alt="foo" /></map> 
).contents();

// $area is the jQuery object for the newly created <area /> tag

$area.attr( coords ); // "50,25,4"

// etc
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签