如何选择< img>元素使用JavaScript编程? [英] How can I select an <img> element programmatically using JavaScript?

查看:126
本文介绍了如何选择< img>元素使用JavaScript编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML文档中有一个< img> ,我想强调,就像用户使用鼠标突出显示它一样。有没有办法使用JavaScript?

I have an <img> in an HTML document that I would like to highlight as though the user had highlighted it using the mouse. Is there a way to do that using JavaScript?

我只需要它在Mozilla中工作,但欢迎任何和所有信息。

I only need it to work in Mozilla, but any and all information is welcome.

编辑:我想要选择图像的原因实际上不是这样,它突出显示,但是我可以使用XPCOM将所选图像复制到剪贴板。因此,img实际上必须被选择才能工作。

推荐答案

这是一个选择第一个图像的例子在页面上(如果您在Firebug中的此页面上测试它将是Stack Overflow标志):

Here's an example which selects the first image on the page (which will be the Stack Overflow logo if you test it out on this page in Firebug):

var s = window.getSelection()
var r = document.createRange();
r.selectNode(document.images[0]);
s.addRange(r)

相关文档:

  • http://developer.mozilla.org/en/DOM/window.getSelection
  • http://developer.mozilla.org/en/DOM/range.selectNode
  • http://developer.mozilla.org/en/DOM/Selection/addRange

这篇关于如何选择&lt; img&gt;元素使用JavaScript编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆