例如,我有 3 个段落:
<p id="txta">AAAAAAAAAAA</p>
<p id="txtb">BBBBBBBBBBB</p>
<p id="txtc">CCCCCCCCCCC</p>
我有一个隐藏它们的脚本,除了一个:
$('#buttonc').click(function () {
$('#txta').hide();
$('#txtb').hide();
$('#txtc').show()
有没有我可以实现的代码,它会自动隐藏 ID 包含“txt”的所有段落(而不是一个一个地隐藏在他自己的行中),然后只显示我要显示的段落?像这样的东西:
$('#buttonc').click(function () {
$('#*includes*txt').hide();
$('#txtc').show();
这可能吗?
最佳答案
您可以在 selector 中使用通配符
. *
将在字符串中搜索 any where
,如果您知道 id 将以 txt 开头,您可以使用 starts with selector for startwith
你需要 ^
而不是 *
$('[id*=txt]').hide();
关于jquery - 隐藏段落ID的html文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020979/