Javascript 获取请求,但不记录任何内容
这是我的代码:Javascript 获取请求,但不记录任何内容,javascript,jquery,Javascript,Jquery,这是我的代码: $.get("http://www.roblox.com/catalog/", function(onWebsite) { console.log($(onWebsite).find('.name.notranslate')[0].attr("href")); }); 每当我运行它时,它不会将任何内容记录到控制台,它只会说:“Object{readyState:1}”。但是,如果我删除.attr(“href”),它就会工作。我的语法有问题吗?也许可以试试: $.get
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log($(onWebsite).find('.name.notranslate')[0].attr("href"));
});
每当我运行它时,它不会将任何内容记录到控制台,它只会说:“Object{readyState:1}”。但是,如果我删除.attr(“href”),它就会工作。我的语法有问题吗?也许可以试试:
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log(onWebsite.find('.name.notranslate')[0].attr("href"));
});
onWebsite不能使用jQuery选择器包装,因为它是从函数返回的。因为它是一个jQuery函数,所以您需要将它与jQuery对象一起使用
使用
根据您当前的代码$(在网站上)。find('.name.notranslate')[0]
将返回没有.attr()方法的底层DOM元素
您可以使用href
属性或方法
或
让我们试试这种方法:
var a;
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
a = $(onWebsite).find('.name.notranslate')[0];
console.log(a);
});
你会得到
<a class="name notranslate" href="/Headless-Horseman-item?id=134082613" title="Headless Horseman">Headless Horseman</a>
或者不创建临时变量
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log($(onWebsite).find('.name.notranslate')[0].href);
});
find是否返回一个对象或对象列表?一个对象,因为我将节点设置为[0]。[0]
返回一个没有任何attr()
方法的DOM节点如果我想获取列表中的下一项怎么办?
var a;
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
a = $(onWebsite).find('.name.notranslate')[0];
console.log(a);
});
<a class="name notranslate" href="/Headless-Horseman-item?id=134082613" title="Headless Horseman">Headless Horseman</a>
var a;
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
a = $(onWebsite).find('.name.notranslate')[0].href
console.log(a);
});
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log($(onWebsite).find('.name.notranslate')[0].href);
});