使用JS闭包不将变量传递给onClick函数(Variable not being passed to onClick function using JS closures)
但我似乎无法使用以下代码。 正如您所看到的,我尝试了很多变化,并且无法将当前值传递给稍后将调用的函数。
当点击div时,我希望警报读取“START”而不是“END”...帮助?
var someString = "START"; document.getElementById('elem1').onclick = function(){ return function(someString){ alert(someString); }(); }; document.getElementById('elem2').onclick = function(){ alert(someString); }; document.getElementById('elem3').onclick = function(){ alert(new String(someString)); }; document.getElementById('elem4').onclick = function(someString){ return function(someString){ alert(someString); }(); }; document.getElementById('elem5').onclick = function(someString){ return function(){ alert(someString); }(); }; document.getElementById('elem6').onclick = function(){ var newSomeString =someString; alert(newSomeString); }; var someString = "END";
<body> <div style="background-color='yellow';display:block;" id="elem1">1</div> <br> <div style="background-color='red';display:block;" id="elem2">2</div> <br> <div style="background-color='blue';display:block;" id="elem3">3</div> <br> <div style="background-color='green';display:block;" id="elem4">4</div> <br> <div style="background-color='orange';display:block;" id="elem5">5</div> <br> <div style="background-color='pink';display:block;" id="elem6">6</div> <br> </body>
I read through Explaining Javascript Scope and Closures
but I can't seem to get the following code to work. As you can see I have tried a lot of variations, and can't pass in the current value to a function that will be called later.
I want the alert to read "START" not "END" when the div is clicked... help?
var someString = "START"; document.getElementById('elem1').onclick = function(){ return function(someString){ alert(someString); }(); }; document.getElementById('elem2').onclick = function(){ alert(someString); }; document.getElementById('elem3').onclick = function(){ alert(new String(someString)); }; document.getElementById('elem4').onclick = function(someString){ return function(someString){ alert(someString); }(); }; document.getElementById('elem5').onclick = function(someString){ return function(){ alert(someString); }(); }; document.getElementById('elem6').onclick = function(){ var newSomeString =someString; alert(newSomeString); }; var someString = "END";
<body> <div style="background-color='yellow';display:block;" id="elem1">1</div> <br> <div style="background-color='red';display:block;" id="elem2">2</div> <br> <div style="background-color='blue';display:block;" id="elem3">3</div> <br> <div style="background-color='green';display:block;" id="elem4">4</div> <br> <div style="background-color='orange';display:block;" id="elem5">5</div> <br> <div style="background-color='pink';display:block;" id="elem6">6</div> <br> </body>
原文:https://stackoverflow.com/questions/28443313
最满意答案
ERROR 0表示MySQL向导无法安装,启动或配置新服务器。 通常删除已安装的实例并将其重置将有助于解决此问题。
尝试
1. explorer中的ONEN mysql
2运行mysql_uninstallservice
3.运行mysql_installservice
Thank you for everyone's help. It seems that I have solved the problem now, although I am not sure what the bug was. I have taken several actions:
- reset password for mysql user
- Used mysqli* method
- used try-catch method in the php script.
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
Cpanel和Nodemailer(Cpanel and Nodemailer)[2022-01-12]
如果应用程序托管在cPanel服务器上,则可以使用host: 'my host', 本地主机而不是实际的dns记录或主机名,因为您在本地连接到邮件服务器。 试一试。 If the app is hosted on the cPanel server, you could use for host: 'my host', localhost instead of your actual dns record or hostname since you connect to the mail server lo ... -
你需要使用mysql命令的shell或php脚本,像这样: #!/bin/sh /usr/bin/mysql -h "server-name" -u "mysql_user" "-pSECRET" -e "UPDATE `users` SET online = `0`" database_name 详细的CPanel cron作业设置可以在这里找到。 You need shell or php script with mysql command, smth like this: #!/bin/sh /us ...
-
不允许使用主机连接远程数据库用户名。 最有可能它被设置为只能从“localhost”连接,所以检查你的mysql服务器上的用户名的详细信息,并查看允许的主机列表 Remote database username is not allowed to connect using your host. Most probably it is set to only be able to connect from "localhost", so check the details of your username ...
-
cPanel mysql错误0(cPanel mysql error 0)[2023-12-10]
ERROR 0表示MySQL向导无法安装,启动或配置新服务器。 通常删除已安装的实例并将其重置将有助于解决此问题。 尝试 1. explorer中的ONEN mysql 2运行mysql_uninstallservice 3.运行mysql_installservice Thank you for everyone's help. It seems that I have solved the problem now, although I am not sure what the bug was. I h ... -
我能够解决它。 这是代码: $con = mysql_connect('localhost', 'serverusername', 'serverpassword'); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $selected = mysql_select_db("db_name",$co ...
-
如何修复cpanel登录错误?(How to fix cpanel log in error?)[2021-11-13]
问题解决了!! 我联系了主办公司,他们能够在几个小时内完成修复。 The issue was solved!! I contacted the host company and they were able to fix it within a few hours. -
对于多行语句,不能使用相同的字符(默认情况下为“;”)来分隔函数定义和整个CREATE语句中的各个语句。 如果您尝试,创建语句将在第一个“;”结束 (在示例代码中,它位于第一个DECLARE )。 由于语句语法错误位于截断语句的末尾,因此错误消息将“'作为发生错误的语句的一部分。 从命令行客户端,您将使用delimiter命令将客户端使用的delimiter更改为(例如)“;;”,然后使用此分隔符结束创建语句: delimiter ;; CREATE FUNCTION F_test (topsbwynum ...
-
MariaDB [test]> create table settest(attrib set('bold','italic','underline') DEF AULT 'bold',color enum('red','green','blue') DEFAULT 'blue'); MariaDB [test]> INSERT INTO settest VALUES('a','s'); Query OK, 1 row affected, 2 warnings (0.14 sec) MariaDB ...
-
似乎MySQL端口没有添加到服务器防火墙中,因为你遇到了这个问题。 请在防火墙中添加端口3306并再次检查。 It's seems that MySQL port is not added in server firewall and due to that you are getting this issues. Please add port 3306 in firewall and check again.