javascript - 如何使用 JavaScript 中的 DOM 操作函数将 <map> 元素添加到文档中?

标签 javascript html dom dictionary

我尝试将 a 添加到 HTML 文档并使用以下代码将其链接到图像:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="de">
<head>
    <title>Website</title>
    <script type="text/javascript">
    function addArea(map, xstart, ystart, xende, yende, idCol, col) {
        area = document.createElement("area");

        area.shape  = "rect";
        area.coords = "" + xstart + ", " + ystart + ", " + xende + ", " + yende + "";
        area.href   = "#" + idCol;
        area.title  = col;
        area.alt    = col;
/*
        area.shape  = "\"rect\"";
        area.coords = "xstart + \", \" + ystart + \", \" + xende + \", \" + yende";
        area.href   = "\"#\" + idCol";
        area.title  = "col";
        area.alt    = "col";

*/      area.setAttribute(
            "onclick",
            "alert(\"Color: \" + col); return false;"
        );

        // append the area to the map
        map.appendChild(area);
    }

    function showMap() {
        idCol = "text";

        // generate the map
        mapCol      = document.createElement("map");
        mapCol.name = "map_" + idCol;
        mapCol.id   = "map_" + idCol;
        addArea(mapCol, 1, 1, 25, 13, idCol, "00FF00");
        addArea(mapCol, 25, 1, 49, 13, idCol, "00FF33");


        imgCol              = document.createElement("img");
        imgCol.src          = "https://www.google.de/images/srpr/logo3w.png";
        imgCol.width        = 275;
        imgCol.height       = 95;
        imgCol.style.border = "1px solid #000";
        imgCol.usemap       = "#name_und_raute_sind_notwendig_bunt_" + idCol;
        imgCol.alt          = "Farbe auswählen";

        imgColArea      = document.createElement("p");
        imgColArea.appendChild(imgCol);

        testcol = "ffffff";
        testlink    = document.createElement("a");
        testlink.appendChild(document.createTextNode("testlink"));
        testlink.setAttribute(
            "onclick",
            "alert(\"Color: \" + testcol); return false;"
        );

        document.getElementById("area").appendChild(imgColArea);
        document.getElementById("area").appendChild(testlink);

        alert("map added with " + mapCol.areas.length + " entries.");

    }


    </script>
</head>
<body onLoad="showMap()">
<div>
before
<div id="area"></div>
after
</div>
</body>

图像应包含链接区域,单击这些区域时会显示提醒文本。不幸的是,这些区域没有显示出来。有人发现我的错误吗?

最佳答案

首先,您的 map 标识符不匹配。此外,属性名称是 useMap,而不是 usemap。使用

imgCol.setAttribute('usemap',"#" + mapCol.name);

imgCol.useMap = "#" + mapCol.name;

相反。您还必须将 map 添加到文档中:

/* ... */
imgColArea.appendChild(imgCol);
imgColArea.appendChild(mapCol);
/* ... */

JSFiddle demonstration

关于javascript - 如何使用 JavaScript 中的 DOM 操作函数将 <map> 元素添加到文档中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147856/

相关文章:

javascript - 具有相同 ID 的 jQuery 子元素

javascript - ember 测试通过 chrome,而不是 phantomjs

html - 响应式导航菜单

html - 如何将文字放在图片之前?

javascript - D3.js:enter().append()后的属性未设置

javascript - 使用 :hover javascript 设置样式

php - 将链接中的文本值定位为一个输出中的多个链接

javascript - jQuery 中的选择器与 this

html - 如何使 HTML <button> 用 CSS 垂直填充表格单元格?

javascript - 将 Javascript 属性连接到 Java 对象