[英]Get content of a div
<select>
<div class="options">
<option value="nothing">Nothing</option>
<option value="snothing">Second Nothing</option>
</div>
</select>
I want to get the content of the DIV and print it inside javascript in a selector. 我想获取DIV的内容并在选择器中的javascript中打印出来。
$(document).ready(function () {
var options = $('.options').html();
.
.
.
var content = '<select>' + options + '</select>';
.
.
.
});
I need to take options from the HTML. 我需要从HTML中获取选项。 I've created a div indise
<select></select>
tags, because I had no other idea. 我创建了一个div
<select></select>
标签,因为我没有其他想法。 But it's not working! 但这不起作用! I tried to put the options var inside
<p></p>
tags so i can print it, it sais 'undefined'. 我试图将选项var放在
<p></p>
标记中,以便我可以打印它,它表示“未定义”。
Assuming the missing >
is a typo, your structure is invalid. 假设缺少的
>
是拼写错误,则您的结构无效。 select
elements cannot have div
elements as direct children . select
元素不能将div
元素作为直接子元素 。
You could do this: 您可以这样做:
<select class="options">
<option value="nothing">Nothing</option>
<option value="snothing">Second Nothing</option>
</select>
And then 接着
var options = $('.options').html();
Firstly, remove the <div>
. 首先,删除
<div>
。 It's not helping you at all. 它根本没有帮助您。 Then, if you want just the options:
然后,如果只需要这些选项:
$("select").html(); // to get the HTML
or 要么
var something = $("select options"); // to get an array of jQuery objects
我想,您想要的是: $('select').html(options)
或$('select').append(options)
,其中options是html字符串,例如"<option value='1'>One</option>"
,或jquery元素集合,例如$('option')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.