简体   繁体   English

Javascript选择嵌套类元素

[英]Javascript select nested class element

I'm working on changing some elements the slider on my website我正在更改我网站上的滑块元素

my slider code looks like this:我的滑块代码如下所示:

<div class="cl1">
    <h1>Some heading</h1>
    <div class="sl_descr">Some description</div>
    <div class="sl_price">from only €00.00<br></div>
</div>

<div class="cl2">
    <h1>Some other heading</h1>
    <div class="sl_descr">Some other description</div>
    <div class="sl_price">from only €00.00<br></div>
</div>

<div class="cl3">
    <h1>yet some heading</h1>
    <div class="sl_descr">yet Some description</div>
    <div class="sl_price">from only €00.00<br></div>
</div>

I would like to change price when the currency changes.我想在货币变化时改变价格。 To do that I would like to use javascript with getElementsByClassName and then innerHTML.为此,我想将 javascript 与 getElementsByClassName 和innerHTML 一起使用。 However my javascript doesn't work as wanted.但是我的 javascript 不能正常工作。 Here it is这里是

document.getElementsByClassName('cl1 sl_price').innerHTML="from only £00.00<br>";

any suggestions to how could I separately address "sl_price" class for every "cl" element?关于如何为每个“cl”元素单独处理“sl_price”类的任何建议? Cheers干杯

getElementsByClassName returns a collection (list) of elements (will therefore not have a innerHTML property) getElementsByClassName返回元素的集合(列表)(因此不会有innerHTML属性)

You could try document.querySelector(".cl1 .sl_price") instead (takes a css selector and returns the first match )您可以尝试document.querySelector(".cl1 .sl_price")代替(采用 css 选择器并返回第一个匹配项

read more at https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorhttps://developer.mozilla.org/en-US/docs/Web/API/Document.querySelector阅读更多

The end result would then be something like this;最终结果将是这样的;

document.querySelector('.cl1 .sl_price').innerHTML = "from only £00.00<br>";

Note: I am assuming you only wanted to match a single element.注意:我假设您只想匹配单个元素。 If not, you should look at @Bommox's answer and querySelectorAll .如果没有,您应该查看@Bommox 的回答和querySelectorAll

document.querySelectorAll('.cl1 .sl_price');

This will select all nested sl_price elements inside cl1.这将选择 cl1 中的所有嵌套 sl_price 元素。 If you are looking to modify more cl's (ie cl1, cl2, cl3) just add a common class to all of them such as cl.如果您希望修改更多 cl(即 cl1、cl2、cl3),只需向所有这些类添加一个公共类,例如 cl。

You should do like this (if not using jQuery):你应该这样做(如果不使用 jQuery):

I have used "for" because I don't you if it may be more than one cl1 or sl_price whithin them.我使用了“for”,因为如果它可能不止一个 cl1 或 sl_price 在它们之中,我不知道。

var nodes = document.getElementsByClassName("cl1");
for(var i = 0; i < nodes.length; i++) {
     var node = nodes[i].getElementsByClassName("sl_price");
     for(var j = 0; j < node.length; i++) {
          node.innerHTML="from only £00.00<br>";
     }
}

If you use Jquery, the code is shorter than in native Javascript.如果您使用 Jquery,则代码比原生 Javascript 短。

$('.cl1 .sl_price').html("from only £00.00<br>");

Here is the fiddle:这是小提琴:

http://jsfiddle.net/4zLbmrLq/ http://jsfiddle.net/4zLbmrLq/

Edit: this is wrong as it doesn't take into account that two class names are given.编辑:这是错误的,因为它没有考虑给出两个类名。

I would suggest using JQuery for something like this, which will make it easier when you try to do something more complex.我建议使用 JQuery 来做这样的事情,当你尝试做一些更复杂的事情时,这会让事情变得更容易。 However the actual problem here is that getElementsByClassName returns an HTMLCollection of elements, and setting innerHTML on it doesn't set it on its elements.然而,这里的实际问题是getElementsByClassName返回一个HTMLCollection元素,并且在其上设置innerHTML并没有在其元素上设置它。 The following code should work:以下代码应该可以工作:

var elements = document.getElementsByClassName('cl1 sl_price');
for (var i = 0; i < elements.length; i++) {
    elements[i].innerHTML = "from only £00.00<br>";
}

I believe what you need to do is something like this:我相信你需要做的是这样的:

document.getElementsByClassName('cl1').getElementsByClassName('sl_price')[0].innerHTML="from only £00.00<br>";

The way you're doing it right now will get elements that have both the classes cl1 and sl_price.您现在的做法将获得同时具有类 cl1 和 sl_price 的元素。 If you chain your methods, you'll be telling it to look for elements with cl1 as a class inside the document object, return an object with that subset, then look inside that subset for elements with the sl_price class.如果你链接你的方法,你会告诉它在文档对象中查找 cl1 作为类的元素,返回一个带有该子集的对象,然后在该子集中查找带有 sl_price 类的元素。 You then have to specify an index because document.getElementsByClassName will return an array of elements matching your requirements.然后您必须指定一个索引,因为 document.getElementsByClassName 将返回一个符合您要求的元素数组。 That array will not have a property array.innerHTML, but the objects contained in its indexes will.该数组不会有属性 array.innerHTML,但包含在其索引中的对象会有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM