简体   繁体   English

子元素按类名错误

[英]Child element by class name error

I am trying to access the element with class ="hs-input" ; 我正在尝试使用类=“ hs-input”访问该元素; this is the basic structure 这是基本结构

<div class = "hbspt-form".......>
 <form ....class="hs-form stacked">
      <div class = "hs_firstname field hs-form-field"...>
          <div class = "input"....>
              <div class="hs-input"...>

This is what I have tried: 这是我尝试过的:

<script type="text/javascript">
var list = document.getElementsByClassName("hbspt-form")[0];
list.style.background = "#fffbd5";
list.style.borderRadius = "5px";
list.style.border= "solid";
list.style.boxShadow = "10px 20px 30px ";
list.style.borderColor = "#f7761f";
list.getElementsByClassName("hs_firstname")[0].style.backgroundColor = "white";
</script>

I am unable to even access the second class(hs-form stacked), Could anyone tell me how to change the background colour of the last class(hs-input) 我什至无法访问第二堂课(HS形式叠放),谁能告诉我如何更改上一堂课的背景色(HS输入)

Note: The console doesn't give any errors if I enter any of the functions but when I execute this it says that the code is not found. 注意:如果输入任何功能,控制台都不会给出任何错误,但是执行此操作时会指出未找到代码。

I am new to javascript and stack overflow, please help with patience.Thanks 我是javascript新手并且堆栈溢出,请耐心帮助。谢谢

Note: This is the code-> http://jsfiddle.net/jg2rek1h/ 注意:这是代码-> http://jsfiddle.net/jg2rek1h/

You can try this: 您可以尝试以下方法:

var list = document.getElementsByClassName("hbspt-form")[0];

var secondclass = list.getElementsByClassName('hs-form')[0];
secondclass.style.background = "#333";

var last_class = list.getElementsByClassName('hs-input')[0];
last_class.style.color = "#94d";

http://jsfiddle.net/k581sL5q/ http://jsfiddle.net/k581sL5q/

I just added the css sheet, check this : http://jsfiddle.net/jg2rek1h/4/ 我刚刚添加了CSS工作表,请检查以下内容: http : //jsfiddle.net/jg2rek1h/4/

If you just want to change the visual outlook use css http://www.w3schools.com/css/ 如果您只想更改外观,请使用css http://www.w3schools.com/css/

If you want to manipulate the look by script, better get along with jQuery for that matter. 如果您想通过脚本来操纵外观,那么最好与jQuery相处。

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

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