Javascript 如果这是一个函数,它可以判断所选元素是否存在于所单击元素的父元素中。我需要帮助调试这个
我正在为谷歌标签管理器制作这个变量。Javascript 如果这是一个函数,它可以判断所选元素是否存在于所单击元素的父元素中。我需要帮助调试这个,javascript,jquery,css-selectors,google-tag-manager,getelementbyid,Javascript,Jquery,Css Selectors,Google Tag Manager,Getelementbyid,我正在为谷歌标签管理器制作这个变量。{{Click Element}}返回元素,例如“ul”。我想找到带有“li”的父元素,并在元素中搜索div.icon-lock是否为子元素。当前代码未返回正确的结果。有人能指出错误或提出更好的方法吗 function(){ var ul = {{Click Element}}; var el =( $("ul").closest("li")) if ( $("el div").
{{Click Element}}
返回元素,例如“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>
课堂直播
系列课程&;模式-1
81分钟
|
8月21日现场直播
通过获取主div包装,然后搜索子类来搜索类
函数hasIconLockClass(){
const iconLock=document.querySelector('ul li div.icon lock');
如果(!iconLock)返回false
返回真值
}
console.log(hasIconLockClass())
课堂直播
系列课程&;模式-1
81分钟
|
8月21日现场直播
请提供HTMLUN。如果ul嵌套在li中,则此逻辑错误地使用了仅向上移动DOM的最近()
。el
(作为字符串)不是有效元素。您可能需要类似于if(el.find('div.icon-lock').length)
的内容。@EugenSunic检查分配的HTML代码。每次单击都将对应于元素中的一个,并且嵌套代码对于每种情况都是相似的。如果代码未正确缩进,则表示歉意。我希望