Javascript 仅显示第一个单词的HTML标题属性

Javascript 仅显示第一个单词的HTML标题属性,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我在jQuery中使用title属性 createAlert: function () { return '<img id="img" class="img pull-left" src="/Content/img/demo_16px.png" tabindex="0" style="outline: none;" title=' + demo.rawData + '></span>'; } createAlert:function(){ 返

我在jQuery中使用title属性

createAlert: function () {            
  return '<img id="img" class="img pull-left" src="/Content/img/demo_16px.png" tabindex="0" style="outline: none;" title=' + demo.rawData + '></span>';
}
createAlert:function(){
返回“”;
}
在此代码中,标题包含动态数据(demo.rawData contain text='this is my code')

但当我在浏览器中悬停标题时,它只显示(即仅显示第一个单词)


有人能告诉我做错了什么吗?

您需要将标题环绕在“like so
title=“”+demo.rawData+”
中,而不是
title='+demo.rawData+”
中,否则呈现的HTML将无效

createAlert: function () {            
  return '<img id="img" class="img pull-left" src="/Content/img/demo_16px.png" tabindex="0" style="outline: none;" title="' + demo.rawData + '"></span>';
}
createAlert:function(){
返回“”;
}

在这里,您可以使用
ES6模板文字提供另一种解决方案

createAlert: function () {            
  return `<img id="img" class="img pull-left" src="/Content/img/demo_16px.png" tabindex="0" style="outline: none;" title="${demo.rawData}"></span>`;
}
createAlert:function(){
返回``;
}

参考文档:

在属性值周围添加引号。