Javascript 我想在php变量上添加VentListener

Javascript 我想在php变量上添加VentListener,javascript,php,Javascript,Php,当点击www.google.com时,我想在变量$html_选项卡上添加VentListener $html_tab .= '<td align="center" valign="middle">'. $row['numeUnitate'] . '</td>'; echo "<script language='javascript'> window.addEventListener('click', fu

当点击www.google.com时,我想在变量$html_选项卡上添加VentListener

    $html_tab .= '<td align="center" valign="middle">'. $row['numeUnitate'] . '</td>';
        echo "<script language='javascript'>
                window.addEventListener('click', function() {
                    var location.href = 'http://www.google.com';
                });
            </script>";
$html\u选项卡=''$行['numeunite']。';
回声“
window.addEventListener('click',function(){
var location.href=http://www.google.com';
});
";

在您的示例中,它可能如下所示:

   <td id="the_id" align="center" valign="middle"> .....

  <script language='javascript'>
   var elem = document.getElementById('the_id');
   elem.addEventListener('click', function() {
                var location.href = 'http://www.google.com';
            });
  </script>";
。。。。。
var elem=document.getElementById('the_id');
元素addEventListener('click',函数(){
var location.href=http://www.google.com';
});
";

好的,在我看到错误后,我理解你的问题

var location.href = 'http://www.google.com';
不要在这里使用keywort变量。 那就行了。 否则浏览器会认为,您需要声明一个新变量

所以,只要写就行了

location.href = 'http://www.google.com';

我不认为该窗口有
点击
事件。请尝试
聚焦
,这可能会满足您的需要。@Doge Yes窗口有
点击
事件您真的希望整个窗口让用户点击一下google.com?您的目标是什么?另一件事是,您不能向php变量添加任何事件。在这种情况下,php只是在回显一个string,浏览器将事件添加到DOM元素。请指定您的问题!