简体   繁体   English

为什么我的JavaScript中的onclick事件处理程序不起作用?

[英]Why doesn't my onclick event handler in Javascript work?

I currently have a button when clicked it should open up a new window but I am getting this error: Uncaught SyntaxError: Unexpected token : . 我目前在单击时有一个按钮,它应该会打开一个新窗口,但是出现此错误: Uncaught SyntaxError: Unexpected token : I am using window.open() 我正在使用window.open()

$url = "http://www.google.com";
$button .= 
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open('.$url.');"/>';

I have noticed that when I use window.open() with no parameters it works but when I pass in the $url variable all hell breaks loose. 我已经注意到,当我使用不带任何参数的window.open()时,它可以工作,但是当我传递$url变量时,所有地狱都会崩溃。 I get syntax errors. 我收到语法错误。 I've tried http://www.google.com , www.google.com and google.com to no avail! 我已经尝试http://www.google.com,www.google.com和google.com无效!

Thanks for your help in advance! 谢谢您的帮助!

Because you are missing the single quotes needed to encapsulate the url string. 因为您缺少封装url字符串所需的单引号。

$url = "http://www.google.com";
$button .= 
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open(\''.$url.'\');"/>';

You're adding a layer of string encapsulation. 您正在添加一层字符串封装。 When you pass a string value to a function it must be in quotes, as it is a string. 当您将字符串值传递给函数时,它必须用引号引起来,因为它是字符串。

doSomething('http://www.ibm.com/');

When you are doing this inline in your html, you need to encapsulate the javascript in double quotes so it becomes 当您在html中进行内联时,您需要将JavaScript封装在双引号中,以使其成为

onclick="doSomething('http://www.ibm.com/');"; 

Then, if you want PHP to echo that, or to assign it as variable, you need to enclose all of that in quotes, so you can do 然后,如果您希望PHP对此进行回显或将其分配为变量,则需要将所有这些都用引号引起来,以便可以

<?php 
    //encapsulate in double quotes and escape double quotes
    echo " onclick=\"doSomething('http://www.ibm.com/');\" "; 

    //encapsulate in single quotes and escape single quotes
    echo ' onclick="doSomething(\'http://www.ibm.com/\'); '; 
 ?>  

Any way you break it down you need have 3 string encapsulations embedded in one another, so you must find a way to differentiate between the quotes used in JS and the quotes used in PHP. 分解的任何方式都需要相互嵌入3个字符串封装,因此您必须找到一种区分JS中使用的引号和PHP中使用的引号的方法。

Otherwise you would have a problem. 否则,您将有问题。

<?php 

    //Houston We have a problem!
    echo " onclick="doSomething('http://www.ibm.com/');\" "; 
         ^         ^                                    ^ ^
 Open Quote       Close Quote                  Open Quote Close Quote 
 ?>  

There are a couple of things wrong here: 这里有几处错误:

You have a period character before the equals, eg 您在等号之前有一个句号,例如

$button .= 

Should be 应该

$button =

And you need to escape your single quotes: 而且您需要转义单引号:

$url = "http://www.google.com";
$button = '<input type="button" value="Print Timetable" class="printButton" onclick="window.open(''.$url.'');"/>';

I think it suppose to be like this: 我认为应该是这样的:

$url = "http://www.google.com";
$button .= 
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open("'.$url.'");"/>';

window.open() require quote or double quote window.open()需要引号或双引号

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

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