【发布时间】:2013-09-28 18:14:35
【问题描述】:
我想知道如何通过类名获取元素,这是html代码
<div class="header" id="parent">
<div class="child"></div>
<div class="head_container">
<img src="images/logo_picture.png" alt="" title="" class="logopic" />
<img src="images/logo_text.png" alt="" title="" class="logotext" />
<img src="images/head_line.jpg" title="" alt="" class="head_line" />
</div>
</div>
这是 jquery,这是个问题,我有带有 getEelementById 的代码,但我想使用 getElementByClass。这是在任何元素上使用不透明度并防止子元素继承不透明度的方法。我会使用五六次类父,所以我需要getElementByClass方法
$(document).ready(function () {
function thatsNotYoChild(parentID) {
var parent = document.getElementById(parentID),
children = parent.innerHTML,
wrappedChildren = '<div id="children" class="children">' + children + '</div>',
x, y, w, newParent, clonedChild, clonedChildOld;
parent.innerHTML = wrappedChildren;
clonedChild = document.getElementById('children').cloneNode(true);
document.getElementById('children').id = 'children-old';
clonedChildOld = document.getElementById('children-old');
newParent = parent.parentNode;
newParent.appendChild(clonedChild);
clonedChildOld.style.opacity = '0';
clonedChildOld.style.filter = 'alpha(opacity=0)';
function doCoords () {
x = clonedChildOld.getBoundingClientRect().left;
y = clonedChildOld.getBoundingClientRect().top;
if (clonedChildOld.getBoundingClientRect().width) {
w = clonedChildOld.getBoundingClientRect().width; // for modern browsers
} else {
w = clonedChildOld.offsetWidth; // for oldIE
}
clonedChild.style.position = 'absolute';
clonedChild.style.left = x + 'px';
clonedChild.style.top = y + 'px';
clonedChild.style.width = w + 'px';
}
window.onresize = function () {
doCoords();
};
doCoords();
}
thatsNotYoChild('parent');
});
【问题讨论】:
-
你有 jQuery 并且你正在使用
document.getElementById等?使用 jQuery 怎么样? -
你有什么理由不使用像 $('.head_container') 或任何类这样的 jquery 吗?
-
没关系,但一切都变得不透明,甚至是子元素,我希望我的标题背景不透明而不是徽标,div 标题内的文本。