jQuery 选择器使用 usemap 属性查找图像时出现问题
我的页面中有一个图像地图:
<div id="books">
<img src="images/books.png" width="330" height="298" border="0" \
usemap="#map_books" />
<map name="map_books" id="map_books" alt="books">
<area shape="poly" coords="17,73,81,288,210,248,254,264, ..." \
href="/about" alt="books" />
</map>
</div>
我有一个尝试使用此地图在文档中查找图像的函数:
function(elemId) { // elemId = "#map_books"
if ($(elemId).attr("tagName") == "MAP") {
// find image using this map
var selector = "img [usemap=\\" + elemId + "]";
var img = $(selector);
if (img.length == 0) {
console.log("Could not find image using " + selector);
}
}
它无法找到图像。
无法使用img找到图像[usemap=\#map_books]
我已经转义了elemId
,因为它以哈希开头 并尝试了选择器的变体。
$("img [usemap$=" + elemId.substring(1) + "]")
$("img").find("[usemap=\\" + elemId + "]")
都找不到图像。有什么想法吗?
I have an image map in my page:
<div id="books">
<img src="images/books.png" width="330" height="298" border="0" \
usemap="#map_books" />
<map name="map_books" id="map_books" alt="books">
<area shape="poly" coords="17,73,81,288,210,248,254,264, ..." \
href="/about" alt="books" />
</map>
</div>
I have a function that tries to find the image in the document using this map:
function(elemId) { // elemId = "#map_books"
if ($(elemId).attr("tagName") == "MAP") {
// find image using this map
var selector = "img [usemap=\\" + elemId + "]";
var img = $(selector);
if (img.length == 0) {
console.log("Could not find image using " + selector);
}
}
It fails to find the image.
Could not find image using img [usemap=\#map_books]
I've escaped the elemId
because it starts with a hash and tried variations of selectors.
$("img [usemap$=" + elemId.substring(1) + "]")
$("img").find("[usemap=\\" + elemId + "]")
Neither find the image. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
img
和[usemap=\#map_books]
之间有一个空格。这会导致尝试将
img
的子元素与设置为#map_books
的属性usemap
进行匹配。你应该使用:
You have a space between
img
and[usemap=\#map_books]
.That results in an attempt to match a child element of
img
with an attributeusemap
set to#map_books
.You should use:
尝试引用 usemap 值的变体:
Try variations on quoting the usemap value:
在 img 和 [] 之间有一个额外的空格,用于获取 img 的属性。
You had an extra space between img and [] for getting to the img's attributes.