为什么我的 JavaScript 代码不工作?
我不断收到此代码的“语法错误:意外标识符”JS 错误:
function hashStuff() {
var messageID = window.location.hash.replace('#inbox-', '');
var msgSubject = $('#subject_' + messageID).html();
setTimeout("readMessage2(" + messageID + ", " + msgSubject + ");", 300);
}
if (window.location.hash) {
setTimeout("hashStuff();", 400);
}
我也尝试过:
if (window.location.hash) {
function hashStuff() {
var messageID = window.location.hash.replace('#inbox-', '');
var msgSubject = $('#subject_' + messageID).html();
setTimeout("readMessage2(" + messageID + ", " + msgSubject + ");", 300);
}
setTimeout("hashStuff();", 400);
}
它们都不起作用。
我试图做的是从元素中获取信息,但我猜页面尚未加载,所以我需要它在一秒钟后触发。我把它放在一个函数中,这样我就可以使用超时,但它不起作用。
有什么想法吗?提前致谢。
I keep getting the "Syntax Error: Unexpected identifier" JS error with this code:
function hashStuff() {
var messageID = window.location.hash.replace('#inbox-', '');
var msgSubject = $('#subject_' + messageID).html();
setTimeout("readMessage2(" + messageID + ", " + msgSubject + ");", 300);
}
if (window.location.hash) {
setTimeout("hashStuff();", 400);
}
I've also tried:
if (window.location.hash) {
function hashStuff() {
var messageID = window.location.hash.replace('#inbox-', '');
var msgSubject = $('#subject_' + messageID).html();
setTimeout("readMessage2(" + messageID + ", " + msgSubject + ");", 300);
}
setTimeout("hashStuff();", 400);
}
Neither of them work.
What I was trying to do was get information from the elements but I guess the page wasn't loaded yet so I need it to trigger after a second. I put it in a function so I can use a timeout and it will not work.
Any ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的 messageID 类似于 1234 并且 msgSubject 是 Hello World,那么正在评估的语句是:
显然,这是不正确的并且会引发错误。
正确的代码是:
If your messageID is something like 1234 and the msgSubject is Hello World, then the statement being evaluated is:
Which, clearly, is incorrect and error-inducing.
The correct code is:
您可以在
$(document).ready(function() {//script here});
中运行脚本。这将确保它在所有元素加载后运行。You can run the script inside
$(document).ready(function() {//script here});
. That will make sure that it is run after all the elements have loaded.尝试将代码包装在
ready
块中:try wrapping your code inside
ready
block: