onClick not working

前端 未结 2 2021
暖寄归人
暖寄归人 2021-02-19 02:39

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


相关标签:
2条回答
  • 2021-02-19 03:14

    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';
    });
    
    0 讨论(0)
  • 2021-02-19 03:14

    As you know, HTML attributes have to be surrounded by quotes, so if you need quotes within the attribute you need a different pairing. Try this:

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

    And if you are within a loop and echo'ing stuff out try PHP's HEREDOC syntax.

    $out = '';
    foreach ( $x as $y )
    {
    $out .= <<<HTML
    <tr onClick="window.location.href='url';">
    HTML;
    }
    echo $out;
    

    Edit: added HEREDOC

    0 讨论(0)
提交回复
热议问题