jQuery td onclick 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15940856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:04:30  来源:igfitidea点击:

td onclick not working

javascriptjqueryhtml-table

提问by Manoz

I am working in MVC4 and using tables, When I set onclick on <td>element of table then related function is not working.

我在 MVC4 中工作并使用表格,当我在<td>表格元素上设置 onclick 时,相关功能不起作用。

This is how I am doing-

这就是我的做法-

 <tr>

      <td style="cursor:pointer" onclick="D();">
       #= DisplayName #</td>
      </tr>

jQuery function-

jQuery 函数-

<script type="text/javascript">

                $(document).ready(function D() {
                    $('td').click(function (event) { loadPartially(event, '@Url.Contact_PartialView_Main()[email protected]') });
                    alert("aa");
                });
            </script>

When this didn't work I tried giving it an ID-

当这不起作用时,我尝试给它一个 ID-

 <tr>

          <td style="cursor:pointer" id="Open">
           #= DisplayName #</td>
          </tr>

jQuery function for this-

用于此的 jQuery 函数-

<script type="text/javascript">

                    $(document).ready(function {
                        $('#Open').click(function (event) { loadPartially(event, '@Url.Contact_PartialView_Main()[email protected]') });
                        alert("aa");
                    });
                </script>

I am trying to get load partial event on click of this element of table.

我试图在单击表的这个元素时获取加载部分事件。

But somehow it is not working. I am not sure if i can use onclick function on <td>element of table.

但不知何故它不起作用。我不确定是否可以在<td>表格元素上使用 onclick 函数。

回答by KingKongFrog

Change:

改变:

$(document).ready(function {

to

$(document).ready(function () {

回答by Feisty Mango

The issue here is that you are mixing an old school approach with a jquery one (admittidly, you are also mixing in syntax that simply won't work).

这里的问题是您将老派方法与 jquery 方法混合在一起(不可否认,您还混合了根本行不通的语法)。

Here is how you would accomplish binding an onclick event absent jquery:

以下是如何在没有 jquery 的情况下完成绑定 onclick 事件:

<script type="text/javascript">
    function D() {
       alert('omg!');
    }
</script>

...

<td onclick="D()">
</td>

Now if you want to use the jquery approach it would look like this:

现在,如果您想使用 jquery 方法,它将如下所示:

<script type="text/javascript">
    $('#IdSelector').click(function() {
        alert('omg!');
    });
</script>

...

<td id="IdSelector">
</td>

With the jquery approach, you sacrifice an easy ability to immediately identify what will happen during the td's onclick for separation of concerns between the javascript logic and the presentation layer (html)

使用 jquery 方法,您牺牲了一种简单的能力来立即识别在 td 的 onclick 期间会发生什么,以分离 javascript 逻辑和表示层 (html) 之间的关注点

I prefer the jQuery approach as it doesn't have to be concerned with a few annoying syntactical issues you have to watch out for when performing inline javascript as well as being much more readable IMO.

我更喜欢 jQuery 方法,因为它不必担心在执行内联 javascript 以及更具可读性的 IMO 时必须注意的一些烦人的语法问题。

EDIT:

编辑:

Demo for jquery approach - http://jsfiddle.net/jSnAh/

jquery 方法的演示 - http://jsfiddle.net/jSnAh/

回答by Deepu

$(document).ready(function(){//You have missed paranthesis of function()
     $('#Open').live('click',function(event) { 
             alert("aa");
             loadPartially(event, '@Url.Contact_PartialView_Main()[email protected]');
      });  
  });

回答by user1491819

onclickon a <td>is valid.

onclick在 a 上<td>是有效的。

Put your alertas the first line of the function D()to ensure it is being called correctly.

将您的alert作为函数的第一行,D()以确保它被正确调用。

Using FireFox, install the Web Developer toolbar and Firebug plugins. Press F12, click the console tab, then click the <td>. If there are any errors, you'll see them in the console tab.

使用 FireFox,安装 Web Developer 工具栏和 Firebug 插件。按 F12,单击控制台选项卡,然后单击<td>. 如果有任何错误,您将在控制台选项卡中看到它们。

回答by Esha Garg

Firstly give a id or class to your td


<tr>

  <td style="cursor:pointer" id='PerformAction'>
   #= DisplayName #</td>
  </tr>


$(document).ready(function (){
$('#PerformAction').click(function (event) { 
    loadPartially(event, '@Url.Contact_PartialView_Main()[email protected]')
    alert("aa");
});
});