javascript中有没有办法在另一个选择器中选择一个选择器,比如在jQuery中:
$("section h1").html();
jQuery在可用时实际使用querySelectorAll。问题是,靠你自己,你永远无法模拟 jQuery 提供的所有优点(例如,filter()、find()、children()、parent()、map()以及not()使用伪类的能力)。
这足以抓取部分中的 h1 元素:
var e = document.querySelector("section h1");
JSF中。
最接近的可能是document.querySelector或document.querySelectorAll。不过应该小心,因为支持仅限于现代浏览器。
您也可以执行以下操作...但是有更简单的方法
见 jsfiddle http://jsfiddle.net/kasperfish/CR5EX/2/
alert('jquery '+$("#section h1").html());
v=document.getElementById("section").getElementsByTagName("h1");
alert('javascript '+v[0].innerHTML);