jquery:在一行中创建多个元素(jquery: create multiple elements in one line)
我有以下示例,其中我将嵌套元素添加到DOM:
$ul = $('<ul>').appendTo($('#test')); $('<li>').text('Item 1').appendTo($ul);<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test"></div>无论如何,我可以在一行中编写两个javascript行。 我试过以下无济于事:
$('<li>').text('Item 1').appendTo($('<ul>')).appendTo($('#test'))请注意, <ul>元素不会被添加: https : //jsfiddle.net/upnchucq/
I have the following example in which I'm adding nested elements to the DOM:
$ul = $('<ul>').appendTo($('#test')); $('<li>').text('Item 1').appendTo($ul);<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test"></div>Is there anyway I can write the two javascript lines in one line. I've tried the following to no avail:
$('<li>').text('Item 1').appendTo($('<ul>')).appendTo($('#test'))Notice the <ul> element does NOT get added: https://jsfiddle.net/upnchucq/
最满意答案
你错了一个括号。
代码应如下所示:
$('<li>').text('Item 1').appendTo($('<ul>').appendTo($('#test')));小提琴: https : //jsfiddle.net/upnchucq/2/
You misplaced one parentheses.
The code should look like:
$('<li>').text('Item 1').appendTo($('<ul>').appendTo($('#test')));Fiddle: https://jsfiddle.net/upnchucq/2/
更多推荐
https,$ul,javascript,电脑培训,计算机培训,IT培训"/> <meta name="descript
发布评论