php 如何在 <a> 标签中调用 onclick 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19299147/
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
how to call a onclick function in <a> tag?
提问by Ankur Dhanuka
i want to open a new window on click of 1
我想点击 1 打开一个新窗口
$leadID = "<a href='javascript:onclick=window.open(lead_data.php?leadid=1, myWin, scrollbars=yes, width=400, height=650);'>1</a>";
it is not showing me error. is there any other way to open new window.
它没有向我显示错误。有没有其他方法可以打开新窗口。
here is the fiddle http://jsfiddle.net/ankurdhanuka/uwypv/
这是小提琴 http://jsfiddle.net/ankurdhanuka/uwypv/
Thanks in advance.
提前致谢。
回答by ?ēēpak
Try onclick function separately it can give you access to execute your function which can be used to open up a new window, for this purpose you first need to create a javascript function there you can define it and in your anchor tag you just need to call your function.
单独尝试 onclick 函数它可以让您访问执行您的函数,该函数可用于打开一个新窗口,为此您首先需要创建一个 javascript 函数,您可以在其中定义它,并在您的锚标记中调用你的功能。
Example:
例子:
function newwin() {
myWindow=window.open('lead_data.php?leadid=1','myWin','width=400,height=650')
}
See how to call it from your anchor tag
看看如何从你的锚标签中调用它
<a onclick='newwin()'>Anchor</a>
Update
更新
Visit this jsbin
访问这个jsbin
http://jsbin.com/icUTUjI/1/edit
http://jsbin.com/icUTUjI/1/edit
May be this will help you a lot to understand your problem.
可能这将有助于您了解您的问题。
回答by Will McChesney
Fun! There are a few things to tease out here:
乐趣!这里有一些事情需要梳理:
$leadID
seems to be a php string. Make sure it gets printed in the right place. Also be aware of all the risks involved in passing your own strings around, like cross-site scriptingand SQL injectionvulnerabilities. There's really no excuse for having Internet-facing production code not running on a solid framework.- Strings in Javascript (like in PHP and usually HTML) need to be enclosed in
"
or'
characters. Since you're already inside both"
and'
, you'll want to escapewhichever you choose.\'
to escape the PHP quotes, or'
to escape the HTML quotes. <a />
elementsare commonly used for “hyper”links, and almost always with ahref
attribute to indicate their destination, like this:<a href="http://www.google.com">Google homepage</a>
.- You're trying to double up on watching when the user clicks. Why? Because a standard click both activates the link (causing the browser to navigate to whatever URL, even that executes Javascript), and “triggers” the onclick event. Tip: Add a
return false;
to a Javascript event to suppress default behavior. - Within Javascript,
onclick
doesn't mean anything on its own. That's becauseonclick
is a property, and not a variable. There has to be a reference to some object, so it knows whoseonclick
we're talking about! One such object iswindow
. You could write<a href="javascript:window.onclick = location.reload;">Activate me to reload when anything is clicked</a>
. - Within HTML,
onclick
can mean something on its own, as long as its part of an HTML tag:<a href="#" onclick="location.reload(); return false;">
. I bet you had this in mind. - Big difference between those two kinds of
=
assignments. The Javascript=
expects something that hasn't been run yet. You can wrap things in afunction
block to signal code that should be run later, if you want to specify some arguments now (like I didn't above withreload
):<a href="javascript:window.onclick = function () { window.open( ... ) };"> ...
. - Did you know you don't even need to use Javascript to signal the browser to open a link in a new window? There's a special target attribute for that:
<a href="http://www.google.com" target="_blank">Google homepage</a>
.
$leadID
似乎是一个php字符串。确保它打印在正确的位置。还要注意传递您自己的字符串所涉及的所有风险,例如跨站点脚本和SQL 注入漏洞。让面向 Internet 的生产代码不在可靠的框架上运行真的没有任何借口。- Javascript 中的字符串(如 PHP 和通常的 HTML)需要用
"
或'
字符括起来。由于您已经在"
和 中'
,您将想要逃离任何您选择的人。\'
转义 PHP 引号,或'
转义 HTML 引号。 <a />
元素通常用于“超级”链接,并且几乎总是带有一个href
属性来指示它们的目的地,例如:<a href="http://www.google.com">Google homepage</a>
。- 当用户点击时,您正试图加倍观看。为什么?因为标准点击会激活链接(导致浏览器导航到任何 URL,即使执行 Javascript),并“触发”onclick 事件。提示:将 a 添加
return false;
到 Javascript 事件以抑制默认行为。 - 在 Javascript 中,
onclick
它本身并不意味着什么。那是因为onclick
是属性,而不是变量。必须有一个对某个对象的引用,所以它知道onclick
我们在谈论谁!一个这样的对象是window
。你可以写<a href="javascript:window.onclick = location.reload;">Activate me to reload when anything is clicked</a>
。 - 在 HTML 中,
onclick
只要它是 HTML 标记的一部分,就可以单独表示某些内容:<a href="#" onclick="location.reload(); return false;">
. 我敢打赌你已经想到了这一点。 - 这两种
=
作业的区别很大。Javascript=
期待一些尚未运行的东西。你可以用的东西在function
以信号代码块应该以后运行,如果你现在要指定一些参数(像我没有以上reload
)<a href="javascript:window.onclick = function () { window.open( ... ) };"> ...
。 - 您知道您甚至不需要使用 Javascript 来通知浏览器在新窗口中打开链接吗?有一个特殊的目标属性:
<a href="http://www.google.com" target="_blank">Google homepage</a>
.
Hope those are useful.
希望这些有用。
回答by Dan
You should read up on the onclick
html attribute and the window.open()
documentation. Below is what you want.
您应该阅读onclick
html 属性和window.open()
文档。下面是你想要的。
<a href='#' onclick='window.open("http://www.google.com", "myWin", "scrollbars=yes,width=400,height=650"); return false;'>1</a>
JSFiddle: http://jsfiddle.net/TBcVN/
JSFiddle:http: //jsfiddle.net/TBcVN/
回答by tymeJV
Use the onclick
as an attribute of your a
, not part of the href
将onclick
用作 的属性a
,而不是href
<a onclick='window.open("lead_data.php?leadid=1", myWin, scrollbars=yes, width=400, height=650);'>1</a>
Fiddle: http://jsfiddle.net/Wt5La/