作为初学者,我对 jQuery 语法很挣扎。 我意识到以下代码没有选择第一个子项(div)。
我的问题: 为什么语法错误?我应该如何选择前一个 anchor 元素中的 div? 请有人能为我回答这个简单的问题或让我走上正确的方向吗?
(我确实搜索过,但找不到答案。) 曼尼提前致谢!
function hilight(a) {
$('a').prev().first-child.css({"backgroundColor":"#ffffff","color":"#000000"}); }
<div>
<a href="#" class="bttn"><div class="bttn">Button</div></a>
<a href="#" class="image" onmouseover="hilight(this)" onmouseout="normal(this)">
<img src="imgage.png"/>
</a></div>
最佳答案
有2个问题
function hilight(a) {
//use a as a variable reference & use .children() to find the first child
$(a).prev().children(':first-child').css({
"backgroundColor": "#ffffff",
"color": "#000000"
});
}
由于您使用的是 jQuery,因此更喜欢使用 jQuery 事件处理程序而不是使用内联处理程序。
演示:Fiddle
关于javascript - jQuery .first-child 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820762/