Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果这是一个函数,它可以判断所选元素是否存在于所单击元素的父元素中。我需要帮助调试这个_Javascript_Jquery_Css Selectors_Google Tag Manager_Getelementbyid - Fatal编程技术网

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 &amp; 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代码。每次单击都将对应于
  • 元素中的一个,并且嵌套代码对于每种情况都是相似的。如果代码未正确缩进,则表示歉意。我希望
  • 中的子代具有div.icon-lock并返回true,否则返回false我不希望使用第一个元素,但如果元素div.icon-lock存在,则可以返回true的函数。我正在为谷歌标签管理器创建一个变量,以应对每次点击。如果单击元素位于关闭列表中,
  • 我想检查同一列表中是否存在div.icon-lock。@Swapnildahood再次检查答案如果单击的元素是“div.flex-post-pic”,则document.querySelector需要定义ul作为父列表,对吗?