将getElementById替换为getELementByClassName/getElementByClass
Replacing getElementById with getELementByClassName/getElementByClass?
例如,目前我正在使用CSS、HTML和Javascript为网站创建功能选项卡,并且我正在使用getElementById
来调用特定的id。然而,现在我想在一个选项卡下添加多个项目,包括图像、更多文本、标题等。如何使用getElementByClassName
或getElementByClass
(我应该使用哪一个?)将所有多个项目分组到一个类中,并将其放入Javascript中?
我想把id改成类,并添加多个元素(我知道怎么做):
<body>
<p id="home">HOME TEXT!!!</p>
/*Like:
<div class="home">
<h3>HOME TITLE!</h3>
<img src="example.jpg">
<p>Welcome to the home page!</p>
</div>
*/
<p id ="about">ABOUT TEXT!!!</p>
<script>
我不知道如何将这个部分(getElementById
)改为处理类:
function HomeTabFunction() {
document.getElementById("home").style.display="block"
document.getElementById("homeTab").style.background = rgb(235, 197, 191)
document.getElementById("about").style.display="none"
document.getElementById("aboutTab").style.background = "lightblue"
}
function AboutTabFunction() {
document.getElementById("about").style.display="block"
document.getElementById("aboutTab").style.background = "blue"
document.getElementById("home").style.display="none"
document.getElementById("homeTab").style.background = "lightblue"
}
</script>
<h1>Little Shop </h1>
<ul id="tabs">
<li><a id="homeTab" href="javascript:void;" onclick="HomeTabFunction()">Home</a></li>
<li><a id="aboutTab" href="javascript:void;" onclick="AboutTabFunction()">About</a></li>
</body>
首先它的getElementsByClassName
而不是getElementByClassName
,在元素的末尾有一个S
。它是复数,因为它返回多个数据。它返回以类名为目标的元素数组。
您必须使用for循环才能使用getElementsByClassName
var classElem = document.getElementsByClassName
for(i=0;i < classElem.length;i++){
if(classElem[i].innerHTML == "home"){
classElem[i].style.display = "block"
classElem[i].style.background = " rgb(235, 197, 191)"
break;
}
}
.innerHTML
返回标签<div>This text is innerHTML</div>
的内容
它被称为getElementsByClassName
,并返回一个必须迭代的HTMLCollection
。
所以更换
document.getElementById("id").whatever.foo = bar;
带有
[].forEach.call(document.getElementsByClassName("class"), function(el) {
el.whatever.foo = bar;
});
首先,getElementById()
在具有给定唯一ID的页面上定位单个元素。
getElementsByTagName()
和getElementsByClassName()
分别返回其标签/类与给定标签/类匹配的元素的阵列。因此,这些并不是简单的可互换。
你为什么不试着把它推广到N标签上呢?使用当前的方法,每次添加新标签时都需要复制代码。
示例:
<!-- These do not need to be paragraphs. Any element will work as long as it has this class and a unique ID -->
<p class="tab-text" id="home">HOME TEXT!!!</p>
<p class="tab-text" id ="about">ABOUT TEXT!!!</p>
<h1>Little Shop </h1>
<ul id="tabs">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
</ul>
<script>
// Run when document is ready
(function() {
// Find the UL parent of the tab links
var tabs = document.getElementById('tabs');
// Find all links inside it
var tabLinks = tabs.getElementsByTagName('a');
// Iterate through them and give them a click event handler
for (var i = 0; i < tabLinks.length; i++)
{
tabLinks[i].onclick = function() {
// Iterate over all the elements with a class of tab-text and hide them initially
[].forEach.call(document.getElementsByClassName("tab-text"), function(el) {
el.style.display = 'none';
});
// Then find the element whose ID matches the href attribute of the link clicked and show that
document.getElementById(this.href.split('#')[1]).style.display = 'block';
return false;
}
}
})();
</script>
如果您想添加一个新的选项卡,唯一需要做的就是创建一个具有"tab text"类和任何id属性的新元素。然后在UL中添加一个新的链接,该链接具有指向ID的href属性。
工作样品:
http://jsfiddle.net/h1cpsudL/
要通过类名获取元素,请使用
...(name){
document.getElementsByClassName(name);
}
但是它返回一个数组,所以应该对返回的每个元素进行迭代。
如果您想使用javascript向元素添加样式,请尝试以下操作:
var el = document.getElementById('theID');
el.className += 'newClassNameToAdd';
使用JQuery。。。
$('#theID').addClass('newClassNameToAdd');
现在,如果你有一个HTMLCollection(多个元素),使用标签或类选择器:
var els = document.getElementsByTagName('tagName');
var len = els.length;
for (var i = 0; i < len; i++) {
els[i].className += 'newClassNameToAdd';
}
使用JQuery。。。
$('tagName').addClass('newClassNameToAdd');
- 没有找到相关文章