Javascript 字符串到 IE 的 html 问题

标签 javascript jquery internet-explorer

我有一个函数可以获取一些通知,循环它们并使用 Jquery 的 prepend 函数将它们添加到 div 中。重要的部分看起来像这样

var myDiv = setNotificationDiv(notif.id, notif.title, notif.message, notif.sentDate, notif.readDate);
$('#leftContent').prepend(myDiv);

setNotificationDiv使用Array.join创建一个包含所有通知信息的div并返回一个字符串

function setNotificationDiv(id, title, message, sentDate, readDate){
    var temp = [];
    temp.push("<div class='notification'><input type='hidden' value='");
    temp.push(id);
    temp.push("' name='id'/><h2>");
    temp.push((title.length > 23 ? title.substring(0,23).concat('...') : title));
    temp.push("</h2><div class='inlineNotification'><p id='sent'>Sent: ");
    temp.push(formatDate(sentDate));
    temp.push("</p>");
    temp.push((readDate != null ? "<img id='read' src='content/images/readNotification.png'/>" : "<img id='read' src='content/images/unreadNotification.png'/>"));
    temp.push("</div><a href='#' class='deleteNotification'><img src='content/images/deleteNotification.png'</a></div>");
    var str = temp.join('');
    return str;
}

问题是在 IE 中,为每个通知添加的都是这样的:

<img src="http://localhost:8080/content/images/deleteNotification.png" a="" <=""/>

但在我测试的所有其他浏览器(Chrome、Firefox 和 Opera)中,通知 div 看起来就像它应该的那样。我不知道为什么 IE 的操作方式如此不同。

任何帮助将不胜感激

谢谢

最佳答案

问题是 HTML 格式错误。 Firefox 和 Chrome 倾向于自动关闭 HTML 元素上任何缺失的关闭标记,但 IE 不会。

这是您的问题:

<img src='content/images/deleteNotification.png'</a>

应该是:

<img src='content/images/deleteNotification.png' /></a>

关于Javascript 字符串到 IE 的 html 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7986093/

相关文章:

javascript - polymer 改变墨水波纹颜色

jquery - 将主题动态更改为 ASP.Net MVC 网站中的网站?

c++ - 构建垂直的 IE 资源管理器栏(带​​对象)(如收藏夹面板)

Javascript 模块继承

javascript - 用于编辑模型的临时 Backbone 集合

javascript - 动态创建的元素上的事件绑定(bind)?

html - IE9 中的可选右栏

HTML 复选框 - 跨浏览器问题

javascript - 无法在 React 中安装 "useHistory"

javascript - $.cookie 不是函数