jQuery 选择器使用 usemap 属性查找图像时出现问题

发布于 2024-08-27 01:32:31 字数 1069 浏览 6 评论 0原文

我的页面中有一个图像地图:

<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 技术交流群。

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

发布评论

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

评论(3

可遇━不可求 2024-09-03 01:32:31

img[usemap=\#map_books] 之间有一个空格。

这会导致尝试将 img 的子元素与设置为 #map_books 的属性 usemap 进行匹配。

你应该使用:

var selector = "img[usemap=\\" + elemId + "]"; 

You have a space between img and [usemap=\#map_books].

That results in an attempt to match a child element of img with an attribute usemap set to #map_books.

You should use:

var selector = "img[usemap=\\" + elemId + "]"; 
辞别 2024-09-03 01:32:31

尝试引用 usemap 值的变体:

$("img[usemap='#whatever']");
$('img[usemap="#whatever"]');

Try variations on quoting the usemap value:

$("img[usemap='#whatever']");
$('img[usemap="#whatever"]');
放赐 2024-09-03 01:32:31
function(elemId) { // elemId = "#map_books"

    if ($('map'+elemId).length) {
        // 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 和 [] 之间有一个额外的空格,用于获取 img 的属性。

function(elemId) { // elemId = "#map_books"

    if ($('map'+elemId).length) {
        // 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);
        }
    }
}

You had an extra space between img and [] for getting to the img's attributes.

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