简体   繁体   English

jQuery的跨度找不到类?

[英]jquery not finding class on span?

I get the html below rendered on my webesite 我在网站上看到了下面的html

<a class="k-grid-filter k-state-active" href="#" tabindex="-1"><span class="k-icon k-filter"></span></a>

I am trying to attach a click handler to the span that is displayed but I cannot get the break-point to hit? 我试图将点击处理程序附加到显示的跨度上,但无法击中断点?

My javascript code is below: 我的JavaScript代码如下:

$(document).on("click", ".k-icon .k-filter", function () {
    debugger;
});

Is there something I have missed here? 我在这里错过了什么吗?

remove space between selector .k-icon and .k-filter 删除选择器.k-icon.k-filter之间的空间

$(document).on("click", ".k-icon.k-filter", function () {
    debugger;
});

if you give space between selector then it considers the second selector as a child. 如果在选择器之间留出空格,则它将第二个选择器视为子代。

To attach a click handler to your span, you can do it the following way. 要将点击处理程序附加到您的跨度上,可以通过以下方式进行。

  1. Add an ID to the span element (assume 'span-id' is the ID here) and bind using an ID 向span元素添加一个ID(假设'span-id'是此处的ID)并使用ID进行绑定

     $('#span-id').on('click', function() { $('#span-id')。on('click',function(){\n  debugger; 调试器;\n}); }); 
  2. In case you absolutely want to select by combination of class names, you need to specify the classnames without spaces, as follows 如果您绝对希望通过类名的组合进行选择,则需要指定不带空格的类名,如下所示

     $('.k-icon.k-filter').on('click', function() { $('。k-icon.k-filter')。on('click',function(){\n  debugger; 调试器;\n}); }); 

Your HTML ELEMENTS STRUCTURE define ( classes place besides elements) : 您的HTML ELEMENTS STRUCTURE定义(类在元素旁边放置):

|-- a  .k-grid-filter .k-state-active
|   |-- span  .k-icon .k-filter

And JQuery elements binding Classes define below(nesting lebel) : 和JQuery元素绑定类定义如下(嵌套lebel):

|-- .k-icon 
|   |-- .k-filter

class '.k-filter' child of '.k-icon' , so your's debugger not working in JQuery. '.k-icon' '.k-filter'子级'.k-filter' ,因此您的调试器无法在JQuery中使用。

Correct JQuery Code : 正确的JQuery代码:

$(document).on("click", ".k-icon.k-filter", function () {
    debugger;
});

`

 $(document).on("click", ".k-icon.k-filter", function () { debugger; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="k-grid-filter k-state-active" href="#" tabindex="-1"> <span class="k-icon k-filter">debugger</span> </a> 

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

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