< TR> onClick不工作 [英] <tr> onClick not working

查看:60
本文介绍了< TR> onClick不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JS将表格行转换为链接。我看起来像这样:

 < tr onClick ='javascript:window.location.href ='url';' > 

然而,当我尝试点击时,它不会按我的意愿进入页面。事实上,点击似乎没有动作。



任何帮助?



编辑:



至于引号,我忘了提到我用PHP回应这个问题。这是我的更新代码:

  echo< tr onClick ='window.location.href ='url?id =。 $ var。 ;>< / TR>中; 

我应该做某种类型的转义,例如 /在这种情况下?

解决方案

首先, 没有 javascript :在事件处理程序中 - 它们包含JavaScript代码,而不是URL。它只是起作用,因为 javascript:是一个在这种情况下标签,因此不是语法错误。
除此之外,任何具有正确语法突出显示的编辑器都会向您显示您正在打破引用,因为您使用HTML属性的单引号和属性内部。 / p>

这是固定代码:

 < tr onclick =window .location.href ='url';> 

除此之外,内联事件处理程序也很脏。使用jQuery更好地附加它们:

  $('tr')。click(function(){
location。 href ='url';
});


I want to turn my table rows into links using JS. I have it looking like this:

<tr onClick='javascript:window.location.href='url';'>

However, when I try to click, it doesn't go the page as I want. In fact, clicking seems to have no action.

Any help?

Edit:

As for the quotes, I forgot to mention that I'm echoing this with PHP. Here's my updated code:

echo "<tr onClick='window.location.href='url?id=" . $var . "';'></tr>";

Should I be doing some sort of escaping like /" in this case?

解决方案

First of all, no javascript: in event handlers - they contain JavaScript code, not an URL. It just works because javascript: is a label in this case and thus not a syntax error. Besides that, any editor with proper syntax highlighting would have shown you that you are breaking the quoting as you are using single quotes for the HTML attribute and inside the attribute.

Here's the fixed code:

<tr onclick="window.location.href = 'url';">

Besides that, inline event handlers are dirty. Better attach them nicely using jQuery:

$('tr').click(function() {
    location.href = 'url';
});

这篇关于&LT; TR&GT; onClick不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆