我有一个 id 列表,其中可能包含空格。我使用了两种方法从 ul 中删除 li(by id):
var element = document.getElementById(<name_of_id>);
$(element).remove();
var element = document.getElementById(<name_of_id>);
element.parentNode.removeChild(element);
这两个都不起作用!请帮忙
这是我的 html :
<li value='A 1' id='A 1'></li>
最佳答案
带有空格的标识符在 HTML 中无效。我建议您使用有效的标识符。
但是,当您需要它们时,您可以使用 Attribute Equals Selector [name=”value”]
$('[id="ID of element"]').remove();
$('[id="remove this li"]').remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>one</li>
<li id="remove this li">two</li>
<li>three</li>
</ul>
关于javascript - 从 ul 中删除 li jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41095670/