我试图搜索元素的后代" service-block"用class" title"找到特定元素使用以下代码。除了" undefined"我无法获得var。分配。任何帮助将不胜感激 -
<div class="col-md-4">
<div id="service-block" class="wp-block default grey-glow">
<div class="figure">
<img alt="..." src="~/images/services/nav-icon-white.jpg" class="img-responsive">
</div>
<div class="wp-block-body services-text">
<h2 id="test" class="title">Lorem Ipsum <span class="heavy-header" style="color:#001990;">Lorem Ipsum</span> Lorem Ipsum</h2>
<p>Lorem Ipsum.</p>
</div>
</div>
</div>
$("#service-block").hover(function () {
var originalHeight = $(this).find('.title').prop("id");
});
答案 0 :(得分:0)
这是你想要的吗?
$("#service-block").hover(function () {
var $elem = $(this).find('.title');
console.log("id of element: ", $elem.attr("id"));
console.log("height of element: ", $elem.height()); //added due to your variable name
});
答案 1 :(得分:0)
尝试将代码放在$(document).ready()
中。然后使用attr
代替prop
$(document).ready(function(){
$("#service-block").hover(function(){
var originalHeight = $(this).find('.title').attr("id");
});
});