动态添加 onmouseover 属性到锚标记

发布于 2025-01-07 00:43:05 字数 667 浏览 1 评论 0原文

我使用从头开始创建的 Java 应用程序从 MS Word 文档生成我的网站 (www.ibiblio.org/britishraj) 上的所有页面。我需要以自动化的方式完成此操作,因为有 35 本大书,每本长达 500 页,总计超过 400 万字、数千个脚注和一千多个图像。

我想添加弹出脚注和弹出图像,同时使我的 html 尽可能不包含 JavaScript。如果我向每个脚注标签和每个图像标签添加 onmouseover= ,我就可以做到这一点。我已经在测试文件中工作了。

456>

需要参考“this”来决定在哪里显示弹出窗口。任何一章可能有多达一百个,每本书二十章,35本书……

那么……如何动态添加onmouseover属性呢?我听说有一些方法可以在加载页面后迭代 DOM,并根据标签的类添加额外的属性,在这种情况下为 clss="fnr"。我还需要传入数字作为参数,在本例中为 456,它是锚标记的 insidehtml。

我见过一些稍微相似但不完全是我需要的例子。我已经为此奋斗了几天,但没有成功。我的 javascript 技能在这里缺乏。

欢迎提供帮助/建议。

.... 现在我聪明了一点,我知道 JQuery 可以轻松地做到这一点。 ....

I generate all the pages on my web site (www.ibiblio.org/britishraj) from MS word documents, with a Java app I created from scratch. I need to do this in an automated fashion as there are 35 large books of up to 500 pages each, totalling over 4 million words, several thousand footnotes, and over a thousand images.

I'd like to add popup footnotes and popup images, while keeping my html as much free of javascript as possible. I can do it if I add an onmouseover= to every footnote tag and every image tag. I have that working in a test file.

<a class="fnr" href="#" onmouseover="showfootnote(456, this);">456</a>>

The rerence to 'this' is needed to decide where to show the popup. There may be as many as one hundred of these in any one chapter, twenty chapters per book, 35 books...

So ... how can I add the onmouseover attribute dynamically? I have heard there is some way of iterating the DOM after loading the page, and adding the extra attribute based on the class of the tag, in ths case clss="fnr". I would also need to pass in the number as a parameter, in this case 456, which is the innerhtml of the anchor tag.

I have seen some examples of things slightly similar but not exactly what I need. I've been struggling with it for a few days and not been successful. My javascript skills are lacking here.

Help/advice would be welcome.

.... Now I am a little wiser I know that JQuery can do this with ease. ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

另类 2025-01-14 00:43:05

将 id 添加到标签中(以便于访问),例如 id="myID"。在要添加 onmouseover 属性的地方,使用:

document.getElementById("myID").onmouseover=/*your function name*/;

以上是 DOM1 兼容的。
对于 DOM2,您可以使用 addEventListener 方法。谷歌一下-_-。

就您的代码而言,您可以迭代所有 ID,并且

document.getElementById("myID").onmouseover=function(){showfootnote(456, document.getElementById("myID"));};

如果您使用上述代码,则使用功能不会改变。 :) 希望这有帮助。

Put an id to the tag(for easy access), for ex, id="myID". Where you want to add the onmouseover attribute, use:

document.getElementById("myID").onmouseover=/*your function name*/;

The above is DOM1 compatibe.
For DOM2, you can use addEventListener methods. Google it -_-.

As far as your code goes specifically, you can iterate over all your IDs and use

document.getElementById("myID").onmouseover=function(){showfootnote(456, document.getElementById("myID"));};

Functionality wont change if you use the above code. :) Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文