如何在JavaScript中获得所有h1,h2,h3等元素? [英] How do I get all h1,h2,h3 etc elements in javascript?

查看:783
本文介绍了如何在JavaScript中获得所有h1,h2,h3等元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javascript中写下这样的内容:

I want to write something like this in javascript:

var all_headings = document.getElementsByTagName("h1,h2,h3,h4,h5,h6");

all_headings 会成为全部列表元素是 h1 h2 h3 ...

all_headings would then be a list of all elements that are h1 or h2 or h3... And in the order that they appear in the document, of course.

我该怎么做?

How do I do it?

推荐答案

使用现代浏览器,您可以执行

With modern browsers you can do

document.querySelectorAll("h1, h2, h3, h4, h5, h6")

或者您可以通过使用jQuery获得跨浏览器兼容性:

Or you could get cross-browser compatibility by using jQuery:

$("h1, h2, h3, h4, h5, h6")

这篇关于如何在JavaScript中获得所有h1,h2,h3等元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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