简体   繁体   English

Onclick HTML does't call Javascript function inside PHP “echo”

[英]Onclick HTML does't call Javascript function inside PHP “echo”

I'm trying to call the Javascript function It's working if I use in Html我正在尝试调用 Javascript function 如果我在 Html 中使用它就可以工作

<input type="button" value="Click Me"  onclick="$.notify('Error: Sign in before Join.', 'error');" class="nk-btn">

But I wanna use that code in Php echo"" but doesn't work但我想在Php echo""中使用该代码但不起作用

echo" <input type='button' value='Click Me'  onclick='$.notify('Error: Sign in before Join.', 'error');' class="nk-btn"> "

because of Using Single quotes insted of Double:因为使用了双引号的单引号:

How to call js function in another way??如何以另一种方式调用js function?

You can use \ to escape quotes in php.您可以使用\来转义 php 中的引号。

echo "<input type='button' value='Click Me'  onclick=\"$.notify('Error: Sign in before Join.', 'error');\" class=\"nk-btn\">"

Your problem is that you are using the same quotes for your onclick and inside the $.notify The same with the class您的问题是您对onclick$.notify相同

you can do it like你可以这样做

echo "<input type='button' value='Click Me' onclick='$.notify('Error: Sign in before Join.', 'error');' class='nk-btn'>"

or或者

echo "<input type='button' value='Click Me' onclick='$.notify('Error: Sign in before Join.', 'error');' class=\"nk-btn\">"

Instead of messing around with escaping quotes in the string, just close the PHP block, write the HTML and open a new PHP block again after it, if needed: Instead of messing around with escaping quotes in the string, just close the PHP block, write the HTML and open a new PHP block again after it, if needed:

// Some PHP
?>

<input type="button" value="Click Me"  onclick="$.notify('Error: Sign in before Join.', 'error');" class="nk-btn">

<?php 
// More PHP

This will not only free you from escaping the quotes, but most IDE's can syntax highlight the HTML as well (which helps with readability and debugging)这不仅可以让您摆脱 escaping 引号,而且大多数 IDE 的语法也会突出显示 HTML(这有助于提高可读性和调试)

However然而

A better way would probably be not to have the onclick attribute on the element at all but rather register an even listener directly in js.更好的方法可能是根本不在元素上使用onclick属性,而是直接在 js 中注册一个甚至监听器。

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

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