Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript DOM在html中添加svg?_Javascript_Html_Css_Dom_Svg - Fatal编程技术网

如何使用JavaScript DOM在html中添加svg?

如何使用JavaScript DOM在html中添加svg?,javascript,html,css,dom,svg,Javascript,Html,Css,Dom,Svg,我想通过DOM操作添加svg图像,而不是在HTMLdiv中添加它们 我通过谷歌搜索尝试了一切,但找不到更好的解决办法。 我想在span元素中插入svg image assets/close.svg,而不是“X”。在“studentDetails”分区内。将感谢您的帮助 HTML: 这可能会有所帮助,因为您可以使用IMG标记简单地添加svg: var studentDetails = document.getElementById('studentDetails'); var favouriteS

我想通过DOM操作添加svg图像,而不是在HTMLdiv中添加它们

我通过谷歌搜索尝试了一切,但找不到更好的解决办法。 我想在span元素中插入svg image assets/close.svg,而不是“X”。在“studentDetails”分区内。将感谢您的帮助

HTML:


这可能会有所帮助,因为您可以使用IMG标记简单地添加svg:

var studentDetails = document.getElementById('studentDetails');
var favouriteStudent = 
  contacts[parseInt(e.currentTarget.id)].favourite ? 'favourite' : '';
  studentDetails.innerHTML =
  "<span id='close-details'><img src='assets/close.svg'></span>" +
  "<span id='favourite' class='" +
  favouriteStudent +
  "'>*</span>" +
  '<div>' +
  contacts[parseInt(e.currentTarget.id)].name +
  '</div>' +
  '<div>' +
  contacts[parseInt(e.currentTarget.id)].email +
  '</div>';

我在第5行进行了编辑,正如您所看到的,我用图像标签替换了X。

您是否尝试过这个问题的答案:我希望它有帮助:
var studentDetails = document.getElementById('studentDetails');
var favouriteStudent = 
    contacts[parseInt(e.currentTarget.id)].favourite ? 'favourite' : '';
studentDetails.innerHTML =
    "<span id='close-details'>x</span>" +
    "<span id='favourite' class='" +
    favouriteStudent +
    "'>*</span>" +
    '<div>' +
    contacts[parseInt(e.currentTarget.id)].name +
    '</div>' +
    '<div>' +
    contacts[parseInt(e.currentTarget.id)].email +
    '</div>';
studentDetails.setAttribute('class', 'hidden');
#close-details {
    display: block;
    float: right;
    font-size: 20px;
    margin-top: -8px;
    background-image: url('assets/close.svg');
    background-repeat: no-repeat;
    background-position: right;
}
var studentDetails = document.getElementById('studentDetails');
var favouriteStudent = 
  contacts[parseInt(e.currentTarget.id)].favourite ? 'favourite' : '';
  studentDetails.innerHTML =
  "<span id='close-details'><img src='assets/close.svg'></span>" +
  "<span id='favourite' class='" +
  favouriteStudent +
  "'>*</span>" +
  '<div>' +
  contacts[parseInt(e.currentTarget.id)].name +
  '</div>' +
  '<div>' +
  contacts[parseInt(e.currentTarget.id)].email +
  '</div>';