为什么我不能从内联锚点调用该对象的函数?
HTML:
<a href="javascript:alert('Hey.')">Alert</a><br/>
<a href="javascript:alert('In anchor:\n' + objection.sustained());">Alert</a>
Javascript:
var objection = {
sustained : (function() {return ("accroches-toi a ton reve")})
};
alert("In script: \n" + objection.sustained());
$("outdat").text( +"<br/>\n");
只是好奇,为什么我不能调用 objection.sutain()
来自锚标记,但是可以从脚本区域执行此操作吗?
使用锚点时似乎甚至不知道该对象的存在。发生在几个主要浏览器中,所以我相信这是设计使然?
HTML:
<a href="javascript:alert('Hey.')">Alert</a><br/>
<a href="javascript:alert('In anchor:\n' + objection.sustained());">Alert</a>
Javascript:
var objection = {
sustained : (function() {return ("accroches-toi a ton reve")})
};
alert("In script: \n" + objection.sustained());
$("outdat").text( +"<br/>\n");
Just curious here, why can't I call objection.sustained()
from an anchor tag, but it's OK to do it from the script region?
It seems to not even know the object exists when using the anchor. Happens in several major browsers so I believe this is by design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 JSFiddle 上使用 No Wrapper(head) 或 No Wrapper(body)
Use No Wrapper(head) or No Wrapper(body) on JSFiddle
变量范围。
Fiddle 将变量放置在文档加载范围中,因此您的锚代码看不到它。正如 @kjy112 提到的,从这些代码块中删除它,一切都应该正常工作。
Variable scope.
Fiddle is placing the variable in the document load scope so you're anchor code can not see it. As @kjy112 mentioned, remove this from those code blocks and all should work fine.