如何更改jQuery对象的文本。我只是想在这里变得更加熟悉。这不起作用:
$(responseFieldHTML).html('hi')
jquery返回这个jQuery对象:
$(responseFieldHTML)
w.fn.init [div.server_response]
这确实有效:
$('.server_response').html('hi')
w.fn.init [div.server_response, prevObject: w.fn.init(1)]
我不理解什么?
仅供参考responseFieldHTML
就是这样:
var responseFieldHTML = `
<div class='server_response'>
</div?
`
早先在DOM上绘制。
答案 0 :(得分:0)
假设responseFieldHTML
是ajax响应,您需要将其存储为对象,然后插入该对象或在修改后将对象解析回html字符串
var responseFieldHTML = `
<div class='server_response'>
</div>
`
// create jQuery object from html string and modify it
var $response = $(responseFieldHTML).html('Hi !!').css('color','red');
// insert as object
$('body').append($response);
// can still modify it after inserted
$response.addClass('new-class')
// or parse modified html to string
console.log($response[0].outerHTML)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>