将 .htc 文件的内容转换为“常规”文件JavaScript
我有一个 .htc 文件,其行为附加到我的页面中的 div (div#test)。在文件内,顶部有一个标签,用于设置行为:
<PUBLIC:ATTACH EVENT="ondocumentready" FOR="element" ONEVENT="function1()" />
在整个文件中,有对“element”、& 的调用。 this.element - 我认为它指的是这个 div#test。
如果我想从这个文件中获取JS,是否可以放入主.html页面中?我尝试在文档加载时调用该函数,但无法使我的语法正确。
我正在尝试:
document.getElementById.('test').attachEvent(onlonad, function1());
如果我做了一些基本错误的事情,或者如果有人能告诉我为什么这样做是一个坏主意,我将不胜感激! =)
I have a .htc file whose behaviour is attached to a div in my page (div#test). Within the file, there is a tag at the top, setting up the behaviour:
<PUBLIC:ATTACH EVENT="ondocumentready" FOR="element" ONEVENT="function1()" />
And throughout the file, there are calls to 'element', & this.element - which I presume are then referring to this div#test.
If I wanted to take the JS from this file, would it be possible to put into the main .html page? I've tried to make calls to the function on document load, but can't get my syntax correct.
I'm trying:
document.getElementById.('test').attachEvent(onlonad, function1());
Would appreciate any pointers, if I'm doing something basic wrong, or if anyone can tell me why doing it at all would be a bad idea! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在错误的位置有一个点,您将一个未定义的变量传递给函数,并且您正在调用
function1()
而不是传递它:正确的语法是
另请注意,只有几个元素支持 onload 事件 - 图像、脚本和正文(映射到 window.onload)。
如果你想在文档加载时调用,那么在 IE 中就很尴尬,因为它不支持其他浏览器支持的文档就绪事件。有很多方法可以解决这个问题,或者您可以使用 window.onload 事件:
You have a dot in the wrong place, you're passing an undefined variable to the function and you're calling
function1()
instead of passing it:Correct syntax would be
Also note that only a few elements support the onload event - images, scripts and the body (which maps to window.onload).
If you want to make calls on document load, then it's awkward in IE because it doesn't support the document ready event that other browsers support. There are ways around this, or you can use the window.onload event: