如果 a 正在搜索一个元素,即。选择框中的选项,如何获取整个选项标签作为html?
<select name="xy">
<option id="xyz" value="abc">something here</option>
some more options
</select>
这不是我想要得到的:
$('select[name="xy"] option[value="abc"]').html()
这只返回我
"something here"
但我想获得整个标签
<option id="xyz" value="abc">something here</option>
在 foreach 函数中,这确实工作得很好
$('option[value="abc"]').each(function(){
console.log(this);
});
返回我的搜索结果
<option id="xyz" value="abc">something here</option>
但是 foreach 一定不是必需的,不是吗?
最佳答案
您可以简单地使用 DOM 元素的 native outerHTML
属性:
$('select[name="xy"] option[value="abc"]')[0].outerHTML
关于javascript - jQuery:如何获取使用选择器找到的整个标签元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870549/