gpt4 book ai didi

javascript - jQuery 从字符串中选择元素

转载 作者:行者123 更新时间:2023-11-30 18:44:35 26 4
gpt4 key购买 nike

我正在处理 ASP.NET 页面。我在显示某些页面时发出 AJAX 请求,它返回一个类似 '<html><head>......</head></html>' 的字符串.我将此字符串设置为元素的内部 html 以显示内容。我想要做的就是只选择并附加一个元素,它的内容来自字符串。是否可以 ?示例:

字符串:

<html><head></head><body><div id='page'>CONTENTS ARE HERE </div></body></html>

我要选择的元素:<div id="page">CONTENTS ARE HERE </div>

有没有办法通过使用 jQuery 或任何其他方式来做到这一点?非常感谢。

最佳答案

您可以使用 .load() 轻松完成此操作:

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

$('#target').load('<url here> #page');

关于javascript - jQuery 从字符串中选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5817170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com