开发者

create elements and set attributes inside each other with jquery?

Answer found :D

I'm new with jQuery and I know there are some really nice quick tricks do doing stuff so.. I know how to answer this question in JavaScript in 30 lines of code.

Current code:

$('<li>, <a>', {
    id: '#tab-' + count
}).html($(this).text()).appendTo('#uls');

I want to add a a tag with a dynamic href. Does that need to be done in a separate chunk of code or can I somehow integrate it with the current code.

What I'm t开发者_开发技巧rying to create:

<li><a href="#tabs-1"> text describing url </a></li>


You can either have a mix out of jQuery element creation + HTML strings or pure element creation:

Mix:

$('<li>', {
    html: '<a href="' + some_variable + '">' + $(this).text() + '</a>',
    id: '#tab-' + count
}).appendTo('#uls');

Methodical:

$('<li>', {
    id: '#tab-' + count
}).append($('<a>', {
    href: some_variable,
    text: $(this).text()
})).appendTo('#uls');


Is there anything wrong with

$('#uls').append(
    $('<li>').append(
        $('<a>', { href: url, text: 'text', id: 'theid'})
    )
);

(I haven't tested it, though...)


one dirty line

$("<li><a href='" + href + "' id='#tab-" + count + "'>" + $(this).text() + "</a></li>").appendTo("#ul");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜