Javascript 使用 jQuery 将 html 添加到 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2774091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:52:47  来源:igfitidea点击:

add html to div with jQuery

javascriptjquerystring

提问by mtwallet

I am trying to add some additional html to a div with id slideshow using jQuery. My code:

我正在尝试使用 jQuery 向带有 id 幻灯片的 div 添加一些额外的 html。我的代码:

$("#slideshow").append("<a id="prev" title="Previous Slide">Previous Slide</a><a id="next" title="Next Slide">Next Slide</a>");

This isn't working, can anyone tell me what I am doing wrong?

这不起作用,谁能告诉我我做错了什么?

回答by SLaks

You're mixing quotes.

你在混合引号。

Your string goes from the first "to the second ", meaning that the string only contains "<a id=". The string is followed by the identifier prev, then another string, creating a syntax error.

您的字符串从第一个"到第二个",这意味着该字符串仅包含"<a id=". 该字符串后跟 identifier prev,然后是另一个字符串,从而产生语法错误。

Change the outer quotes around the string to ', like this:

将字符串周围的外部引号更改为',如下所示:

'<a id="prev" title="Previous Slide">Previous Slide</a><a id="next" title="Next Slide">Next Slide</a>'

回答by mcgrailm

your not escaping your quotes best way to fix is to put the append in single quotes

你没有转义你的引号最好的解决方法是将附加放在单引号中

$("#slideshow").append('<a id="prev" title="Previous Slide">Previous Slide</a><a id="next" title="Next Slide">Next Slide</a>');