我无法弄清楚如何在JavaScript中使用多个ID.单个ID和getElementById没问题,但是只要我将ID更改为类并尝试使用getElementsByClassName,该函数就会停止工作.我读过有关该主题的100篇帖子; 仍然没有找到答案,所以我希望有人知道如何使getElementsByClassName工作.
以下是我用于测试的一些简单代码:
function change(){
document.getElementById('box_one').style.backgroundColor = "blue";
}
function change_boxes(){
document.getElementsByClassName ('boxes').style.backgroundColor = "green";
}
<input name="" type="button" onClick="change(document.getElementById('box_one')); change_boxes(document.getElementsByClassName('boxes'))" value="Click" />
<div id="box_one"></div>
<div class="boxes" ></div>
<div class="boxes" ></div>
Run Code Online (Sandbox Code Playgroud) 如何计算最适合使用rem设置的字体大小的行高?
例如:
html {
font-size: 62.5%;
}
p {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
font-size: 1.4rem;
line-height: 1.3; /* how do i calculate this value? */
}
Run Code Online (Sandbox Code Playgroud)
我问这个问题的原因是,我很困惑如何理解体型中的字体大小(便于重新计算),实际的rem字体大小和行高的"非值"之间的关系,就我而言在STATIC布局中理解表示如下字体大小:
font-size: 20px;和line-height: 2.0;-将增加字体大小的高度,行高
在流畅的布局中 - 当使用字体大小的rem时 - 将是"非值"行高:2.0; 使用在rem中计算的字体高度作为行高还是仍然依赖于基于像素的值(在示例中是旧浏览器的后备)?
我想我应该说这是我原来的问题 - 我现在要编辑
我有几个内部具有相同文本的按钮 - 每个按钮在单击时打开包含div.问题是,我无法仅在单击的SINGLE按钮上进行文本更改 - 当我单击按钮时,文本会同时在所有按钮上更改.
有没有人知道如何仅在点击的按钮中更改文本?
这是我到目前为止的代码 - jQuery无法正常工作:
HTML
<div class="btn"> SHOW </div>
<div class="pre_hide" style="background: blue">
<h5>BOX 1</h5>
</div> <!-- END OF pre_hide -->
<br>
<div class="btn"> SHOW </div>
<div class="pre_hide" style="background: blue">
<h5>BOX 2</h5>
</div> <!-- END OF pre_hide -->
<br>
<div class="btn"> SHOW </div>
<div class="pre_hide" style="background: blue">
<h5>BOX 3</h5>
</div> <!-- END OF pre_hide -->
Run Code Online (Sandbox Code Playgroud)
JQuery的
$('.btn').click(function(){
if(event.target === this) { // THIS IS NOT WORKING !!
if ($(this).text() == "HIDE") {
$('.btn').text("SHOW");
$(this).next('.pre_hide').css({'display': 'none'}); …Run Code Online (Sandbox Code Playgroud) 我有 2 个名为“按钮”的 div,里面有另一个 div。我想更改点击时的可见性:
<div class="button">
<div class="content"> 1 </div>
</div>
<div class="button">
<div class="content"> 2 </div>
</div>
Run Code Online (Sandbox Code Playgroud)
我将 class="show" 与 display: 块一起应用,以在单击它时显示“内容”。
但是如何从已经可见的 div 中删除 class='show' 并在单击时使新的 div 可见?
应该是这样的:
$('.button').click(function() {
$('.content').removeClass('show');
// This line wont work - only here to show what im trying to accomplish..
$(this).(".content").addClass("show");
});
Run Code Online (Sandbox Code Playgroud)