简体   繁体   English

使用Jquery .find()遍历以按类和存储属性选择元素

[英]Traversing Using Jquery .find() to Select Element by Class & Store Prop

I'm trying to search through the descendants of the element "service-block" to find the specific element with class "title" using the below code. 我正在尝试使用以下代码搜索元素“ service-block”的后代,以找到具有“ title”类的特定元素。 I can't get the var to have anything but "undefined" assigned. 我无法让var分配“ undefined”以外的任何内容。 Any help would be much appreciated - 任何帮助将非常感激 -

<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");
});

Is this what you want? 这是你想要的吗?

$("#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
});

Try to put your code inside $(document).ready() . 尝试将您的代码放入$(document).ready() Then use attr instead of prop 然后使用attr代替prop

$(document).ready(function(){
   $("#service-block").hover(function(){
     var originalHeight = $(this).find('.title').attr("id");
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM