所以我最近开始对页面进行编程...有点小事情,我正在尝试之前在页面中完成的一些操作,所以我只是复制了代码并更改了值。我想知道是否有人可以遮蔽一些光线,为什么它在我的 Opera 浏览器中工作正常,但在我的 Chrome 或 Firefox 上却不能(我在我的计算机上使用 wamp 作为本地网络服务器)
我所做的是使用一个元素并改变它的值(用户选择日期)调用一个使用ajax并调用数据库并返回内容的函数,我认为不需要详细信息)
所以我让下面的
<select name="daySelector" id="daySelectorId" onchange="changeDate(daySelector.value)">
<option value="-1">Day</option>
<option value="2012-01-30">2012-01-30</option>
<option value="2012-01-31">2012-01-31</option>
<option value="2012-02-01">2012-02-01</option>
<option value="2012-02-02">2012-02-02</option>
<option value="2012-02-03">2012-02-03</option>
<option value="2012-02-04">2012-02-04</option>
<option value="2012-02-05">2012-02-05</option>
<option value="2012-02-06">2012-02-06</option>
<option value="2012-02-07">2012-02-07</option>
<option value="2012-02-08">2012-02-08</option>
<option value="2012-02-09">2012-02-09</option>
<option value="2012-02-10">2012-02-10</option>
<option value="2012-02-11">2012-02-11</option>
<option value="2012-02-12">2012-02-12</option>
<option value="2012-02-13">2012-02-13</option>
</select>
Opera工作得很好并返回了我想要的东西.. 。
但是 Chrome 和 Firefox 由于某种原因说 daySelector 没有定义......当我使用“this”引用时,所有浏览器都工作得很好。有趣的是,我从中获取代码的项目是我在更新我的电脑之前所做的,该电脑只安装了 Firefox(旧版本),我很确定它不会造成任何问题......
所以任何想法。我意识到这本身不是问题,但我对这样的细节很好奇,我想知道...顺便说一句,对于那些在 html/ajax/js/... 中编程了几个月以上的人...我应该传递整个元素并在函数中获取我需要的内容吗?
So I have recently started programming pages... Little thins and I was trying something in a page that I had done previously so I just copied the code and changed the values. I was wondering if anyone can shade some light why it worked fine in my Opera browser but not on my Chrome or Firefox (I was using wamp for my computer act as a localwebserver)
What I did was using a element and the change of it's value (the user selecting a date) to call a function that went to use ajax and call a database and return stuff, I don't think the details are needed)
So I had the following
<select name="daySelector" id="daySelectorId" onchange="changeDate(daySelector.value)">
<option value="-1">Day</option>
<option value="2012-01-30">2012-01-30</option>
<option value="2012-01-31">2012-01-31</option>
<option value="2012-02-01">2012-02-01</option>
<option value="2012-02-02">2012-02-02</option>
<option value="2012-02-03">2012-02-03</option>
<option value="2012-02-04">2012-02-04</option>
<option value="2012-02-05">2012-02-05</option>
<option value="2012-02-06">2012-02-06</option>
<option value="2012-02-07">2012-02-07</option>
<option value="2012-02-08">2012-02-08</option>
<option value="2012-02-09">2012-02-09</option>
<option value="2012-02-10">2012-02-10</option>
<option value="2012-02-11">2012-02-11</option>
<option value="2012-02-12">2012-02-12</option>
<option value="2012-02-13">2012-02-13</option>
</select>
Opera worked just fine and returned what I wanted...
But Chrome and Firefox for some reason say that daySelector is not defined... When I use the 'this' reference all browsers work just fine. The funny thing is that the project I took the code from was something I did before I update my pc which had only firefox installed (an older version that is) and I am pretty sure it didn't cause any problems...
So any ideas. I realize this is not a problem per se but I am curious about details like this and I wanted to know... BTW, to those that have programmed in html/ajax/js/... for more than a few months... Should I just pass the whole element and take what I need inside the function?
发布评论
评论(4)
Opera 可能引用
document.daySelector.value
,而 FF 和 Chrome 则引用window.daySelector.value
。替换为this.value
它将绑定到节点。Opera is likely referencing
document.daySelector.value
whereas FF and Chrome are referencingwindow.daySelector.value
. Replace withthis.value
it will be bound to the node.将 onchange 替换为:
onchange="changeDate(this.value)"
daySelector 未在该上下文中定义。 Opera 必须为您处理它,否则就会默默地消耗该错误。
Replace your onchange with:
onchange="changeDate(this.value)"
daySelector isn't defined in that context. Opera must handle it for you or silently be consuming the error.
替换
成
这个应该可以正常工作
Replace
with
this should work fine
提交的答案是正确的,但是我强烈建议您查看 jQuery,因为这会简单得多,而且是跨浏览器的。
例如:
我明白这可能看起来有点复杂,但如果你把它分解就会有意义,这就是为什么我提出了明确的评论。
一旦您掌握了 jQuery 的要点,您将能够将上述语句压缩为:
只需将其放入您的 javascript 代码中并从 html 中删除 'onchange=changeDate(day...' 属性。
您无需担心此功能无法跨浏览器工作,
请务必查看 jQuery 文档以了解如何实现它。同样,使用 Google Code 的快速示例非常简单(不要忘记获取 API)。来自 Google 的密钥,已找到链接如下):
上面的代码只是将 jQuery 加载到您的网站中,并将其放入您想要使用它的所有网页中
:http://code.google.com/apis/loader/signup.html
希望这有帮助
The submitted answers are correct, however I would urge you to look at jQuery as this would be a lot simpler and it is cross browser.
For example:
I appreciate that this may seem a little complex, but if you break it down it will make sense, that is why I have put in clear comments.
Once you have 'got the jist' of jQuery, you will be able to compress the above statement into:
Just place this into your javascript code and remove the 'onchange=changeDate(day...' attribute from your html.
You will not need to worry about this not working cross browser
Make sure you look at the jQuery documentation to learn how to implement it. Again, it is very easy. Quick example using Google Code (dont forget to get an API key from Google, link found below):
The above will simply load jQuery into your website. Place this into all of your webpages that you would like to use it.
Get your API key here: http://code.google.com/apis/loader/signup.html
Hope this helps