该脚本使用 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 )
最佳答案
您想要使用 getAttribute
而不仅仅是字段访问器。
var someLink = document.createElement("A");
someLink.href = "/foo";
someLink.href; // => "http://whatever.com/foo"
someLink.getAttribute("href"); // => "/foo"
关于jquery - 使用 JQuery 时如何停止 jsdom 自动限定 href 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098443/