将 .htc 文件的内容转换为“常规”文件JavaScript

发布于 2024-09-30 22:42:08 字数 483 浏览 6 评论 0原文

我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2024-10-07 22:42:08

您在错误的位置有一个点,您将一个未定义的变量传递给函数,并且您正在调用 function1() 而不是传递它:

document.getElementById.('test').attachEvent(onlonad,  function1());
//                     ^ this                ^   ^  ^ and these ^^

正确的语法是

document.getElementById('test').attachEvent("onload", function1);

另请注意,只有几个元素支持 onload 事件 - 图像、脚本和正文(映射到 window.onload)。

如果你想在文档加载时调用,那么在 IE 中就很尴尬,因为它不支持其他浏览器支持的文档就绪事件。有很多方法可以解决这个问题,或者您可以使用 window.onload 事件:

window.onload = function () {
    // Code to execute when the window is loaded here 
}

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:

document.getElementById.('test').attachEvent(onlonad,  function1());
//                     ^ this                ^   ^  ^ and these ^^

Correct syntax would be

document.getElementById('test').attachEvent("onload", function1);

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:

window.onload = function () {
    // Code to execute when the window is loaded here 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文