语法缺失)当所有)似乎在那里?

时间:2021-09-18 00:45:13

Okay, so I've been building this website. One of the pages is a newsfeed www.wenotelling.x10.mx/news/ The news page has the newsfeed page embedded within it, which is where the actual updating happens. All that is very well and good, and that webpage seems to work. I was having 2 issues though.

好的,所以我一直在建立这个网站。其中一个页面是新闻源www.wenotelling.x10.mx/news/新闻页面中嵌入了新闻源页面,这是实际更新发生的地方。所有这一切都非常好,并且该网页似乎有效。我有两个问题。

  1. The newsfeed didn't seem to update when the pages is loaded, but loads when the page is refreshed.
  2. 加载页面时,新闻源似乎没有更新,但在页面刷新时加载。
  3. I wanted to add smileys.
  4. 我想添加表情符号。

So I wrote some JScript to fix both of these issues in one go. Since the newsfeed is updated through the HTML page, and I didn't want to have to hunt for the smiley's URL every time, I came up with the following code: http://www.wenotelling.x10.mx/news/smilescript.js

所以我写了一些JScript来一次解决这两个问题。由于新闻源是通过HTML页面更新的,而且我不想每次都要搜索笑脸的URL,我想出了以下代码:http://www.wenotelling.x10.mx/news/smilescript .js文件

$(document).ready(function() 
{
    alert("Called");
    document.getElementByClass('happy').innerHTML = '<img src="smileys/happy.gif"></img>';
    document.getElementByClass('star').innerHTML = '<img src="smileys/star.gif"></img>';
    document.getElementByClass('dead').innerHTML = '<img src="smileys/dead.gif"></img>';
    document.getElementByClass('yawn').innerHTML = '<img src="smileys/yawn.gif"></img>';
    document.getElementByClass('snub').innerHTML = '<img src="smileys/snub.gif"></img>';
    document.getElementByClass('relax').innerHTML = '<img src="smileys/relax.gif"></img>';
    document.getElementByClass('devil').innerHTML = '<img src="smileys/devil.gif"></img>';
    document.getElementByClass('cool').innerHTML = '<img src="smileys/cool.gif"></img>';
    document.getElementByClass('wink').innerHTML = '<img src="smileys/wink.gif"></img>';
    document.getElementByClass('shock').innerHTML = '<img src="smileys/shock.gif"></img>';
    document.getElementByClass('bigsmile').innerHTML = '<img src="smileys/bigsmile.gif"></img>';
    document.getElementByClass('confused').innerHTML = '<img src="smileys/confused.gif"></img>';
    document.getElementByClass('sad').innerHTML = '<img src="smileys/sad.gif"></img>';
    document.getElementByClass('angry').innerHTML = '<img src="smileys/angry.gif"></img>';
    document.getElementByClass('clown').innerHTML = '<img src="smileys/clown.gif"></img>';
    document.getElementByClass('blush').innerHTML = '<img src="smileys/blush.gif"></img>';
    if(location.hash !="#");
    {
        location = "#";
        location.reload(true);
    }
    alert("A-OK");
});

So, when editing we can simply put <smiley class="happy"></smiley>. I know smiley isn't a real tag, but I didn't think it'd make a difference because I've seen "faketags" before. I tried changing the smiley tag to <div class="happy"></div> but that didn't work either. At the end of the script, I put a refresh function.

所以,在编辑时我们可以简单地把 。我知道笑脸并不是真正的标签,但我认为它不会有所作为,因为我之前看过“faketags”。我尝试将笑脸标签更改为

,但这也无效。在脚本的最后,我放了一个刷新功能。

Anyways, the script wasn't working. It's being called, but not working. So I ran it through Firebug. Firebug gave the following message:

无论如何,脚本无法正常工作。它被称为,但没有工作。所以我通过Firebug运行它。 Firebug发出以下消息:

SyntaxError: missing ) after argument list
[Break On This Error]   
(22 out of range 21)

I then looked at the script, and well, all of the ) seem to be there. Not only that, but the smileys prior to line 22 aren't working either.

然后我看了一下脚本,好吧,所有的东西似乎都在那里。不仅如此,第22行之前的表情也不起作用。

Anyways. Any ideas why: 1) The Smileys aren't appearing? 2) The script isn't being fully executed?

无论如何。任何想法为什么:1)笑脸没有出现? 2)脚本没有完全执行?

3 个解决方案

#1


5  

In the script you linked to, line #19:

在您链接到的脚本中,第19行:

if(location.hash !="#");{

That semi-colon shouldn't be there. (btw, the lines inside the if block should be indented)

那个分号不应该在那里。 (顺便说一下,if块内的行应该缩进)

Furthermore, there is no such a thing as document.getElementsByClass, only document.getElementsByClassName, and that returns a NodeList and not a single element (so changing its innerHTML won't do much.)

此外,没有document.getElementsByClass这样的东西,只有document.getElementsByClassName,并且返回NodeList而不是单个元素(因此更改其innerHTML不会做太多。)

Other notes:

其他说明:

  • In your website, you are including css/stylesheet.css twice (lines #4 and #12), and actually 3 times, if you count the one inside the iframe.

    在你的网站中,你包括css / stylesheet.css两次(第4行和第12行),实际上是3次,如果算上iframe中的那个。

  • The jquery version you're using is incredibly outdated, and the inclusion of jquery is hardly justified in this case - you're just using it to do a $(document).ready, which can be done without jquery if you ether stick a (semi-close) window.onload listener, or just put the script close to the </body>

    你正在使用的jquery版本是非常过时的,在这种情况下包含jquery是不合理的 - 你只是用它来做一个$(文档).ready,这可以在没有jquery的情况下完成(半关闭)window.onload监听器,或者只是将脚本放在 附近

  • As said in the comments, and I couldn't agree more, don't just stick images inside those elements. Add an appropriate CSS class, and let CSS handle it.

    正如评论中所说,我不能同意,不要只是将图像粘贴在这些元素中。添加一个合适的CSS类,让CSS处理它。

#2


5  

Sometimes javascript errors are a bit misleading, but at least one syntax error can be spotted in your code:

有时javascript错误有点误导,但在您的代码中至少可以发现一个语法错误:

 if(location.hash !="#");{

has an extra ;. Maybe that's the reason.

有一个额外的;也许这就是原因。

Btw, since you're using jQuery anyway, why not use it for element selection and html content setting?

顺便说一下,既然你正在使用jQuery,为什么不用它来进行元素选择和html内容设置呢?

#3


0  

A couple things you should immediately fix would be a call to a non-existent method, and a stray-semicolon. The first is your call to getElementByClass, which is singular rather than plural. And Secondly, you have a semicolon out of place here: if(location.hash !="#");.

你应该立即解决的一些事情是调用一个不存在的方法和一个流浪分号。第一个是你对getElementByClass的调用,它是单数而不是复数。其次,你在这里有一个不对的分号:if(location.hash!=“#”);.

Since you're using jQuery, I'd encourage you to avoid native methods like getElementsByClass, since that doesn't exist in some older browsers. Instead, try something like this:

由于您使用的是jQuery,我建议您避免使用getElementsByClass等本机方法,因为在某些旧版浏览器中不存在这种方法。相反,尝试这样的事情:

var emoticons = ["happy","star","dead","yawn"];

$(document).ready(function () {
    $.each(emoticons, function (index, value) {
        $("<img>", {src: "smileys/" + value + ".gif"}).appendTo("." + value);
    });
});​

Note here how we don't repeat ourselves over and over for each emoticon. I would encourage you to look into sprites, since they generally work far better as a solution for problems like this. But that's for another day.

请注意我们如何不为每个表情重复一遍又一遍。我鼓励你研究精灵,因为它们通常可以更好地解决这类问题。但那是另一天。

Demo: http://jsfiddle.net/yRSF9/

演示:http://jsfiddle.net/yRSF9/

#1


5  

In the script you linked to, line #19:

在您链接到的脚本中,第19行:

if(location.hash !="#");{

That semi-colon shouldn't be there. (btw, the lines inside the if block should be indented)

那个分号不应该在那里。 (顺便说一下,if块内的行应该缩进)

Furthermore, there is no such a thing as document.getElementsByClass, only document.getElementsByClassName, and that returns a NodeList and not a single element (so changing its innerHTML won't do much.)

此外,没有document.getElementsByClass这样的东西,只有document.getElementsByClassName,并且返回NodeList而不是单个元素(因此更改其innerHTML不会做太多。)

Other notes:

其他说明:

  • In your website, you are including css/stylesheet.css twice (lines #4 and #12), and actually 3 times, if you count the one inside the iframe.

    在你的网站中,你包括css / stylesheet.css两次(第4行和第12行),实际上是3次,如果算上iframe中的那个。

  • The jquery version you're using is incredibly outdated, and the inclusion of jquery is hardly justified in this case - you're just using it to do a $(document).ready, which can be done without jquery if you ether stick a (semi-close) window.onload listener, or just put the script close to the </body>

    你正在使用的jquery版本是非常过时的,在这种情况下包含jquery是不合理的 - 你只是用它来做一个$(文档).ready,这可以在没有jquery的情况下完成(半关闭)window.onload监听器,或者只是将脚本放在 附近

  • As said in the comments, and I couldn't agree more, don't just stick images inside those elements. Add an appropriate CSS class, and let CSS handle it.

    正如评论中所说,我不能同意,不要只是将图像粘贴在这些元素中。添加一个合适的CSS类,让CSS处理它。

#2


5  

Sometimes javascript errors are a bit misleading, but at least one syntax error can be spotted in your code:

有时javascript错误有点误导,但在您的代码中至少可以发现一个语法错误:

 if(location.hash !="#");{

has an extra ;. Maybe that's the reason.

有一个额外的;也许这就是原因。

Btw, since you're using jQuery anyway, why not use it for element selection and html content setting?

顺便说一下,既然你正在使用jQuery,为什么不用它来进行元素选择和html内容设置呢?

#3


0  

A couple things you should immediately fix would be a call to a non-existent method, and a stray-semicolon. The first is your call to getElementByClass, which is singular rather than plural. And Secondly, you have a semicolon out of place here: if(location.hash !="#");.

你应该立即解决的一些事情是调用一个不存在的方法和一个流浪分号。第一个是你对getElementByClass的调用,它是单数而不是复数。其次,你在这里有一个不对的分号:if(location.hash!=“#”);.

Since you're using jQuery, I'd encourage you to avoid native methods like getElementsByClass, since that doesn't exist in some older browsers. Instead, try something like this:

由于您使用的是jQuery,我建议您避免使用getElementsByClass等本机方法,因为在某些旧版浏览器中不存在这种方法。相反,尝试这样的事情:

var emoticons = ["happy","star","dead","yawn"];

$(document).ready(function () {
    $.each(emoticons, function (index, value) {
        $("<img>", {src: "smileys/" + value + ".gif"}).appendTo("." + value);
    });
});​

Note here how we don't repeat ourselves over and over for each emoticon. I would encourage you to look into sprites, since they generally work far better as a solution for problems like this. But that's for another day.

请注意我们如何不为每个表情重复一遍又一遍。我鼓励你研究精灵,因为它们通常可以更好地解决这类问题。但那是另一天。

Demo: http://jsfiddle.net/yRSF9/

演示:http://jsfiddle.net/yRSF9/