如果这是一个函数,可以判断所选元素是否存在于被单击元素的父元素中。我需要帮助调试这个
If this is a function that can tell if the selected element is present in parent element of the clicked element. I need help debugging this
我正在为 Google 跟踪代码管理器创建此变量。 {{Click Element}}
returns 元素让我们说“ul”。我想找到 "li"
的父元素,并且在元素中,我想搜索 div.icon-lock
是否是后代。当前代码未返回正确的结果。谁能指出错误或提出更好的方法?
function(){
var ul = {{Click Element}};
var el =( $("ul").closest("li"))
if ( $("el div").hasClass("icon-lock") ) {
return true
}
else {
return false
}
}
我正在使用我的函数的 HTML 代码之一
<li _ngcontent-bvv-c8="" tabindex="0" id="5f2abd67d480440d11d7cec1">
<div _ngcontent-bvv-c8="" class="cie-accordion-heading">
<!---->
<span _ngcontent-bvv-c8="" class="badge-red mr-3">Live Class
</span>
<!---->Class on Series & Patterns - 1
</div>
<div _ngcontent-bvv-c8="" class="cie-accordion-item flex-post">
<!---->
<div _ngcontent-bvv-c8="" class="flex-post-pic">
<!---->
<img _ngcontent-bvv-c8="" alt="video-img" src="angular/assets/img/course-experience/course-video.svg">
<!---->
<!---->
<!---->
<!---->
</div>
<div _ngcontent-bvv-c8="" class="flex-post-content text-light-sm text-truncate">
<!---->
<!---->
<!---->
<span _ngcontent-bvv-c8="">81 mins
</span>
<!---->
<!---->
<span _ngcontent-bvv-c8="" class="px-3">|
</span>
<span _ngcontent-bvv-c8="" class="text-light-sm">Was live on Aug 21
</span>
</div>
<!---->
<div _ngcontent-bvv-c8="" class="icon-lock">
</div>
</div>
通过获取主 div 包装器搜索 class,然后搜索后代 class。
function hasIconLockClass() {
const iconLock = document.querySelector('ul li div .icon-lock');
if (!iconLock) return false
return true
}
console.log(hasIconLockClass())
<ul>
<li _ngcontent-bvv-c8="" tabindex="0" id="5f2abd67d480440d11d7cec1">
<div _ngcontent-bvv-c8="" class="cie-accordion-heading">
<!---->
<span _ngcontent-bvv-c8="" class="badge-red mr-3">Live Class
</span>
<!---->Class on Series & Patterns - 1
</div>
<div _ngcontent-bvv-c8="" class="cie-accordion-item flex-post">
<!---->
<div _ngcontent-bvv-c8="" class="flex-post-pic">
<!---->
<img _ngcontent-bvv-c8="" alt="video-img" src="angular/assets/img/course-experience/course-video.svg">
<!---->
<!---->
<!---->
<!---->
</div>
<div _ngcontent-bvv-c8="" class="flex-post-content text-light-sm text-truncate">
<!---->
<!---->
<!---->
<span _ngcontent-bvv-c8="">81 mins
</span>
<!---->
<!---->
<span _ngcontent-bvv-c8="" class="px-3">|
</span>
<span _ngcontent-bvv-c8="" class="text-light-sm">Was live on Aug 21
</span>
</div>
<!---->
<div _ngcontent-bvv-c8="" class="icon-lock">
</div>
</div>
</li>
</ul>
我正在为 Google 跟踪代码管理器创建此变量。 {{Click Element}}
returns 元素让我们说“ul”。我想找到 "li"
的父元素,并且在元素中,我想搜索 div.icon-lock
是否是后代。当前代码未返回正确的结果。谁能指出错误或提出更好的方法?
function(){
var ul = {{Click Element}};
var el =( $("ul").closest("li"))
if ( $("el div").hasClass("icon-lock") ) {
return true
}
else {
return false
}
}
我正在使用我的函数的 HTML 代码之一
<li _ngcontent-bvv-c8="" tabindex="0" id="5f2abd67d480440d11d7cec1">
<div _ngcontent-bvv-c8="" class="cie-accordion-heading">
<!---->
<span _ngcontent-bvv-c8="" class="badge-red mr-3">Live Class
</span>
<!---->Class on Series & Patterns - 1
</div>
<div _ngcontent-bvv-c8="" class="cie-accordion-item flex-post">
<!---->
<div _ngcontent-bvv-c8="" class="flex-post-pic">
<!---->
<img _ngcontent-bvv-c8="" alt="video-img" src="angular/assets/img/course-experience/course-video.svg">
<!---->
<!---->
<!---->
<!---->
</div>
<div _ngcontent-bvv-c8="" class="flex-post-content text-light-sm text-truncate">
<!---->
<!---->
<!---->
<span _ngcontent-bvv-c8="">81 mins
</span>
<!---->
<!---->
<span _ngcontent-bvv-c8="" class="px-3">|
</span>
<span _ngcontent-bvv-c8="" class="text-light-sm">Was live on Aug 21
</span>
</div>
<!---->
<div _ngcontent-bvv-c8="" class="icon-lock">
</div>
</div>
通过获取主 div 包装器搜索 class,然后搜索后代 class。
function hasIconLockClass() {
const iconLock = document.querySelector('ul li div .icon-lock');
if (!iconLock) return false
return true
}
console.log(hasIconLockClass())
<ul>
<li _ngcontent-bvv-c8="" tabindex="0" id="5f2abd67d480440d11d7cec1">
<div _ngcontent-bvv-c8="" class="cie-accordion-heading">
<!---->
<span _ngcontent-bvv-c8="" class="badge-red mr-3">Live Class
</span>
<!---->Class on Series & Patterns - 1
</div>
<div _ngcontent-bvv-c8="" class="cie-accordion-item flex-post">
<!---->
<div _ngcontent-bvv-c8="" class="flex-post-pic">
<!---->
<img _ngcontent-bvv-c8="" alt="video-img" src="angular/assets/img/course-experience/course-video.svg">
<!---->
<!---->
<!---->
<!---->
</div>
<div _ngcontent-bvv-c8="" class="flex-post-content text-light-sm text-truncate">
<!---->
<!---->
<!---->
<span _ngcontent-bvv-c8="">81 mins
</span>
<!---->
<!---->
<span _ngcontent-bvv-c8="" class="px-3">|
</span>
<span _ngcontent-bvv-c8="" class="text-light-sm">Was live on Aug 21
</span>
</div>
<!---->
<div _ngcontent-bvv-c8="" class="icon-lock">
</div>
</div>
</li>
</ul>