1

该脚本使用 jsdom 和 jquery 来获取 a 标签的 href 属性的值。出于某种原因,相对于我在其中运行脚本的路径,它完全合格。我怎样才能获得 href 值,而不是完全合格的?

var currentDoc = jsdom.jsdom('<html><head><title>href test</title></head><body><p><a href="test.html">Test</a></p></body></html>';, null, {});
var window = currentDoc.createWindow();
jsdom.jQueryify(window, 'jquery-1.4.2.min.js' , function() {
    console.log(window.$('a')[0]['href']);
});

(代码片段也在https://gist.github.com/2355968

4

1 回答 1

1

您想使用getAttribute而不是仅使用字段访问器。

var someLink = document.createElement("A");
someLink.href = "/foo";
someLink.href; // => "http://whatever.com/foo"
someLink.getAttribute("href"); // => "/foo"
于 2012-04-11T00:50:31.560 回答