<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<ul>
<li><strong>list</strong> item 1 -
one strong tag</li>
<li><strong>list</strong> item <strong>2</strong> -
two <span>strong tags</span></li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
<script type="text/javascript">
$('li').filter(function(index) {
return $('strong', this).length == 1;
}).css('background-color','red');
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
鉴于上面的HTML,下面的选择器中逗号的含义是什么?
return $('strong', this).length == 2;
Run Code Online (Sandbox Code Playgroud)
如果我删除"强"这个词会怎么样?
我在文档的不同部分有匹配的文本.第一个是表中的一组"标签",如下所示:
<div id="my-div">
<div><a href="#">tag 1</a></div>
<div><a href="#">tag 2</a></div>
</div>
Run Code Online (Sandbox Code Playgroud)
然后在文档的其他几个部分中,当我想要突出显示匹配链接时,我有一个隐藏元素,如下所示:
<div class="hide-me">tag 1</div>
Run Code Online (Sandbox Code Playgroud)
然后我的点击功能是这样的:
$('#my-div a').click(function() {
var txt = $(this).text();
console.log(txt);
});
Run Code Online (Sandbox Code Playgroud)
输出是一个空字符串,但我不知道为什么.
我想隐藏一个元素,如果它的父元素没有某个类:
<li class="current_page_parent">
<a href="parent.html">Parent</a>
<ul class="children">
<li>foo</li>
<li>bar</li>
</ul>
</li>
Run Code Online (Sandbox Code Playgroud)
jQuery("ul.children").hide();
Run Code Online (Sandbox Code Playgroud)
目前总是隐藏<ul class="children">不分类.我想关闭它只有父母是:not一个<li class="current_page_parent">.
jQuery("ul.children:not(:parent.current_page_ancestor)").hide();
Run Code Online (Sandbox Code Playgroud)
没有运气.
如标题中所述,我想找到类似:contains()但匹配精确字符串的东西.换句话说,它不应该是部分匹配.例如,在这种情况下:
<div id="id">
<p>John</p>
<p>Johny</p>
</div>
Run Code Online (Sandbox Code Playgroud)
$("#id:contains('John')")将同时匹配John和Johny,而我想只匹配John.
提前致谢.
编辑: ES2015单线解决方案,万一有人找到它:
const nodes = [...document.querySelectorAll('#id > *')].filter(node => node.textContent === 'John');
console.log(nodes);Run Code Online (Sandbox Code Playgroud)
/* Output console formatting */
.as-console-wrapper { top: 0; }
.as-console { height: 100%; }Run Code Online (Sandbox Code Playgroud)
<div id="id">
<p>John</p>
<p>Johny</p>
</div>Run Code Online (Sandbox Code Playgroud)
如何<tr>使用jQuery 定位表中的第二个?我使用.closest或nth-child?
// something like this..
var MyLocation = $('.myclass').closest('tr').after('tr'); // fixed
<table class ='myclass'>
<tr>
<td></td>
</tr>
<!-- put some thing here -->
<tr>
</tr>
Run Code Online (Sandbox Code Playgroud) 我在UL内部有一堆LI,每个都有一个唯一的ID.
给定两个ID,选择两个相应LI的最佳方法是什么,以及两者之间的所有LI?
谢谢!
我有一个动态列表,需要选择前一项.
<ul class="album">
<li id='li-1'></li>
<!-- ... -->
<li id='li-8'></li>
<li id='li-9'></li>
<li class='drop-placeholder'>drag your favorites here</li>
</ul>
var lastLiId = $(".album li:last").attr("id"); // minus one?
Run Code Online (Sandbox Code Playgroud) 我如何用jQuery 选择下面的第一个<p>元素<div>?
<div>
<h1>heading</h1>
<p>How do I select this element with jQuery?</p>
<p>Another paragraph</p>
</div>
Run Code Online (Sandbox Code Playgroud) 我有很多图像元素,并希望获得特定图像的来源,其中替代文本是"示例".
我试过这个:
var src = $('.conversation_img').attr('src');
Run Code Online (Sandbox Code Playgroud)
但我不能得到那个我需要的那个.
如何根据其替代文本选择特定图像元素?