我想通过其 ID 从另一个站点获取 div 的内容。
比方说,我有一个网站,我想从这个 URL 获取 id 为“mainbar”的 div 内的所有内容 -https://stackoverflow.com/questions https://stackoverflow.com/questions
你能告诉我,如何用原生 javascript 或 jquery 制作它吗?
提前致谢。
如果您有权访问其他网站并且Access-Control-Allow-Origin
header 存在,那么 jQuery 可以像这样使用:
$.get('https://stackoverflow.com/questions').then(function (html) {
// Success response
var $mainbar = $(html).find('#mainbar');
document.write($mainbar.html());
}, function () {
// Error response
document.write('Access denied');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)