JQuery - Unfamiliar notation
I am modifying a javascript file in which they have used the following code. Does anyone know what this does / where it开发者_StackOverflow社区 is documented / etc. It appears it is creating an anchor node and giving it the inner html of "Back", but I'm not sure how it works or what it's capabilities are, as I need to add various attributes to the link:
$("<a id=>").html("Back");
Thanks!
jQuery is just being forgiving. Normally, the code would look like this, instead:
$('<a/>').html("Back");
Which means, create an a
element and set its inner HTML to "Back". You can chain some attribute assignments directly after:
$('<a/>')
.html('Back');
.attr('id', 'your-id');
This code is indeed creating an anchor element:
<a id="">Back</a>
You can add attributes using the "attr" function, like so:
$("<a id=>").html("Back").attr('href', myUrl);
Alternatively, you can add the attributes directly in the markup:
$("<a id='myId' href='url'>").html("Back");
It is creating an anchor element, but it hasn't appended it to anything, what you would normally do, is either:
$("body").append($("<a>").html("Back").attr("target", "_blank"));
(as an example), or even:
$("<a>").html("Back").attr("target", "_blank").appendTo($("body"));
Because it is a jQuery object, you can continue chaining methods on it to build it up how you want to.
I think it's just some bothched HTML being passed to the factory. Should result in a jQuery collection holding one anchor element which is not yet in the DOM and which has an empty id attr and contains the text "Back":
<a id="">Back</a>
精彩评论