I have string
(not document object) which contain html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" style="display:none;">
<!-- P {margin-top:0;margin-bottom:0;} -->
</style>
</head>
<body dir="ltr">
bla bla
</body>
</html>
and some text or other html code, which I need to insert before end of body tag in first html. In which way is better to do this? - Convert string to html? - Insert text or html to it - Convert result to string
答案 0 :(得分:1)
试试这个: -
var newHTML = "<div><p>This is New Html</p></div>";
$("body").append(newHTML);
console.log($("body").html());
如果您需要在HTML中连接另一个字符串,请尝试以下方法: -
var firstString = "<div><p>This is a string</p></div>";
var secondString = "<p>This is another string</p>";
firstString += secondString;
console.log(firstString);
<强>更新强>
function AddHtml() {
var firstString = "<body><div><p>This is a string</p></div></body>";
var indexOfbody = firstString.indexOf("</body>");
var Firsthalf = firstString.slice(0, indexOfbody);
var middleHalf= "<div><p>This is another string</p></div>";
var LastHalf = firstString.slice(indexOfbody);
var FinalString = Firsthalf + middleHalf+ LastHalf;
console.log(FinalString);
}
答案 1 :(得分:0)
Try this
var htmlString = "<p>Hello World</P>";
$(document.body).append(htmlString);
答案 2 :(得分:0)
Simply do .append()
$(document).ready(function () {
$('body').append('Some content <b>with HTML!</b>');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Existing code here<br/>