簡體   English   中英

使用Jquery .find()遍歷以按類和存儲屬性選擇元素

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

我正在嘗試使用以下代碼搜索元素“ service-block”的后代,以找到具有“ title”類的特定元素。 我無法讓var分配“ undefined”以外的任何內容。 任何幫助將非常感激 -

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

這是你想要的嗎?

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

嘗試將您的代碼放入$(document).ready() 然后使用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