如何在JavaScript中通过文本更改切换按钮?
我想在div中隐藏和显示溢出内容。如果我单击expand bautton,它将展开。但我想这个按钮应该是切换和文本也改变。Thnks如何在JavaScript中通过文本更改切换按钮?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想在div中隐藏和显示溢出内容。如果我单击expand bautton,它将展开。但我想这个按钮应该是切换和文本也改变。Thnks 函数描述HeightMore(){ var myElement=document.querySelector(“.backwhite”); myElement.style.height=“自动”; } .backwhite{ 背景色:浅绿色; 填充:15px; 高度:50px; 溢出:隐藏; } 一,。创建电子邮件活动的用户界面 二,。创建电子邮件活动的功能
函数描述HeightMore(){
var myElement=document.querySelector(“.backwhite”);
myElement.style.height=“自动”;
}
.backwhite{
背景色:浅绿色;
填充:15px;
高度:50px;
溢出:隐藏;
}
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
扩大
既然您在jQuery中标记了问题,
使用jQuery的toggleClass函数可以轻松实现这一点
工作示例
使用javascript尝试以下操作,首先检查高度,然后确定:
函数描述HeightMore(){
var myElement=document.querySelector(“.backwhite”);
如果(myElement.style.height==“自动”){
myElement.style.height=“50px”;
}否则{
myElement.style.height=“自动”;
}
}
.backwhite{
背景色:浅绿色;
填充:15px;
高度:50px;
溢出:隐藏;
}
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
切换
使用jquery方法切换
例:
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“p”).toggle();
});
});
这是一段
在隐藏()和显示()之间切换
$(“.btn”)。单击(函数(){
$(“.backwhite”).toggleClass(“溢出”)
$(“.backwhite”).hasClass(“溢出”)?$(.btn”).text(“展开”):$(.btn”).text(“压缩”)
})
.backwhite{
背景色:浅绿色;
填充:15px;
高度:50px;
}
.溢出{
溢出:隐藏;
}
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
扩大
这里有一个使用纯JavaScript的解决方案
首先,您应该创建一个名为.expanded
的新类,我们可以打开和关闭它-
.backwhite.expanded {
height: auto;
}
接下来,我修改了JavaScript以切换.expanded
类以及按钮的内容-
function toggleDescriptionHeight(e) {
// Toggle .expanded class
document.querySelector(".backwhite").classList.toggle('expanded');
// Toggle textContent of the button
e.target.textContent == 'Expand' ? e.target.textContent = 'Collapse' : e.target.textContent = 'Expand';
}
// Add event listener to button click
var button = document.querySelector('.btn');
button.addEventListener('click', toggleDescriptionHeight)
链接到CodePen上的工作示例-尝试此代码。点击按钮时,它将更改溢出和按钮值。 祝你好运
<html>
<head>
</head>
<body>
<script>
function descriptionHeightMore() {
var myElement = document.querySelector(".backwhite");
myElement.style.height = "auto";
if (myElement.style.overflow == "hidden")
myElement.style.overflow = "visible";
else
myElement.style.overflow = "hidden";
var myButton = document.getElementsByTagName("button");
if (myButton.value == "expand")
myButton.value = "collapse";
else
myButton.value = "expand";
}
</script>
<style>
.backwhite {
background-color: aqua;
padding: 15px;
height: 50px;
overflow: hidden;
}
</style>
<div class="container">
<div class="backwhite">
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
</div>
<button type="button" class="btn btn-default btn-block" onclick="descriptionHeightMore();">Expand</button>
</div>
</body>
</html>
.backwhite{
背景色:浅绿色;
填充:15px;
高度:50px;
溢出:隐藏;
}
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
扩大
通过javascript获取元素的高度是不可能的。您有用于更改高度的按钮,因此检查其按钮文本很容易进行切换更改
功能描述高度更多(t){
var myElement=document.querySelector(“.backwhite”);
如果(t.innerHTML==“展开”){
myElement.style.height=“自动”;
t、 innerHTML=“折叠”;
}
否则{
myElement.style.height=“50px”;
t、 innerHTML=“展开”;
}
}
.backwhite{
背景色:浅绿色;
填充:15px;
高度:50px;
溢出:隐藏;
}
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
扩大
伙计们,看看吧
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.backwhite{
background-color: aqua;
padding:15px;
height:50px;
overflow:hidden;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1 /jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var myElement = document.querySelector(".backwhite");
if(myElement.style.height == "auto"){
myElement.style.height = "50px";
}else{
myElement.style.height = "auto";
}
});
});
</script>
</head>
<body>
<div class="backwhite">
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
</div>
<button type="button">Toggle</button>
</body>
</html>
.背景白{
背景色:浅绿色;
填充:15px;
高度:50px;
溢出:隐藏;
}
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
var myElement=document.querySelector(“.backwhite”);
如果(myElement.style.height==“自动”){
myElement.style.height=“50px”;
}否则{
myElement.style.height=“自动”;
}
});
});
一,。创建电子邮件活动的用户界面
二,。创建电子邮件活动的功能
三,。使用拖放功能创建关键字显示
一,。创建电子邮件活动的用户界面
二,。创建函数
function toggleDescriptionHeight(e) {
// Toggle .expanded class
document.querySelector(".backwhite").classList.toggle('expanded');
// Toggle textContent of the button
e.target.textContent == 'Expand' ? e.target.textContent = 'Collapse' : e.target.textContent = 'Expand';
}
// Add event listener to button click
var button = document.querySelector('.btn');
button.addEventListener('click', toggleDescriptionHeight)
<html>
<head>
</head>
<body>
<script>
function descriptionHeightMore() {
var myElement = document.querySelector(".backwhite");
myElement.style.height = "auto";
if (myElement.style.overflow == "hidden")
myElement.style.overflow = "visible";
else
myElement.style.overflow = "hidden";
var myButton = document.getElementsByTagName("button");
if (myButton.value == "expand")
myButton.value = "collapse";
else
myButton.value = "expand";
}
</script>
<style>
.backwhite {
background-color: aqua;
padding: 15px;
height: 50px;
overflow: hidden;
}
</style>
<div class="container">
<div class="backwhite">
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
</div>
<button type="button" class="btn btn-default btn-block" onclick="descriptionHeightMore();">Expand</button>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.backwhite{
background-color: aqua;
padding:15px;
height:50px;
overflow:hidden;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1 /jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var myElement = document.querySelector(".backwhite");
if(myElement.style.height == "auto"){
myElement.style.height = "50px";
}else{
myElement.style.height = "auto";
}
});
});
</script>
</head>
<body>
<div class="backwhite">
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
</div>
<button type="button">Toggle</button>
</body>
</html>