是否无法在title属性中使用HTML实体?

时间:2011-05-04 21:39:54

标签: javascript html html-entities

上面的screengrab来自Firefox。光标悬停在图像左侧的黄点上。它是一个<img>元素(实际上它是一个图像以及一个包含单个圆形<area>元素的图像映射,但我认为这种区别并不重要)已经在JavaScript中创建和设置了样式,包括标题属性的应用(通过剪切和粘合字符串构造)。我怎样才能让它表现出来并显示预期的角色,而不是&ndash;?它适用于innerHTML(左上角左侧的文章“Barrow-In-Furness”是一个使用JavaScript创建的div及其innerHTML集。)

编辑:回答Domenic的问题:这是构建和应用title属性的JavaScript函数(除了执行其他工作):

var StyleLinkMarker = function (LinkNumber, EltA, EltI) {
    var AltText = LocationName[LinkStart[LinkNumber]] +
                  " to " +
                  LocationName[LinkEnd[LinkNumber]];
    if (!EltA) {
        EltA = document.getElementById("link_marker_area" + LinkNumber);
        EltI = document.getElementById("link_marker_img" + LinkNumber);
    }
    if (LinkStatus[LinkNumber] === 9) {
        var CanBuyLinkCode = BoardPreviewMode ? 0 : CanBuyLink(LinkNumber);
        if (CanBuyLinkCode === 0) {
            EltI.src = ImagePath + "icon-buylink-yes.png";
            AltText += " (you can buy this " + LinkAltTextDescription + ")";
        } else {
            EltI.src = ImagePath + "icon-buylink-no.png";
            AltText += " (you cannot buy this " + LinkAltTextDescription;
            AltText += CanBuyLinkCode === 1 ?
                       ", because you aren't connected to it)" :
                       ", because you would have to buy coal from the Demand Track, and you can't afford to do that)";
        }
    } else if ( LinkStatus[LinkNumber] === 8 ||
                (LinkStatus[LinkNumber] >= 0 && LinkStatus[LinkNumber] <= 4)
                ) {
        EltI.src = ImagePath + "i" + LinkStatus[LinkNumber] + ".png";
        if (LinkStatus[LinkNumber] === 8) {
            AltText += " (orphan " + LinkAltTextDescription + ")";
        } else {
            AltText += " (" +
                       LinkAltTextDescription +
                       " owned by " +
                       PersonReference(LinkStatus[LinkNumber]) +
                       ")";
        }
    } else {
        throw "Unexpected Link Status";
    }
    EltA.alt = AltText;
    EltA.title = AltText;
};

LocationName如下:

var LocationName = [
    "Barrow&ndash;In&ndash;Furness", "Birkenhead",                "Blackburn", "Blackpool",
                           "Bolton",    "Burnley",                     "Bury",     "Colne",
                   "Ellesmere Port",  "Fleetwood",                "Lancaster", "Liverpool",
                     "Macclesfield", "Manchester",             "The Midlands", "Northwich",
                           "Oldham",    "Preston",                 "Rochdale",  "Scotland",
                        "Southport",  "Stockport", "Warrington &amp; Runcorn",     "Wigan",
                        "Yorkshire"
];

2 个答案:

答案 0 :(得分:5)

您没有设置标题属性,您要设置标题属性,它需要文本而不是HTML(尽管setAttribute方法也需要文本字符串)。

一般来说,在处理DOM操作时,您提供的是文本而不是HTML。 .innerHTML是此规则的明显例外。

答案 1 :(得分:1)

这是一种从HTML转换为文本的简便方法:

function convertHtmlToText(value) {
    var d = document.createElement('div');
    d.innerHTML = value;
    return d.innerText;
}

然后您的代码可以更新为:

EltA.title = convertHtmlToText(AltText);