如何从另一个网站通过ID获取Element [英] How to get Element by Id from Another website

查看:74
本文介绍了如何从另一个网站通过ID获取Element的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个站点获取div的ID.

I would like to get the content of div by its ID from another site.

比方说,我有一个网站,我想从该URL中获取div内具有"mainbar" ID的所有内容- https://stackoverflow.com/questions

Let's say, I have a site and I want to get everything that is inside of div with the id of "mainbar" from this URL - https://stackoverflow.com/questions

您能告诉我,如何使用本机javascript或jquery制作它吗?

Could you tell me, how to make it with native javascript or jquery?

谢谢.

推荐答案

如果您可以访问其他站点并且存在Access-Control-Allow-Origin标头,则可以像这样使用jQuery:

If you have access to the other site and the Access-Control-Allow-Origin header is present, then jQuery can be used like this:

$.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>

这篇关于如何从另一个网站通过ID获取Element的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆