html text change,html - Javascript - Change text function question - Stack Overflow

本文探讨了如何使用JavaScript实现点击箭头时容器展开与折叠,并同步改变箭头方向的功能。作者已成功实现容器的展开与折叠,但希望进一步完善功能,使箭头在点击时能够改变方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Newbie javascript question: I have created a container and have coded them so that when the arrow is clicked the container expands/collapses to show the content. However, I would like to add a feature in which the arrow's direction also changes when clicked.

Here is an overview of what I am looking for:

on click, expand container to show hidden content

change the arrow from down-facing arrow (code= ∨) to up facing arrow (code=∧)

apply the same css styling to the up-facing arrow

on click, collapse container to hide content

change the arrow from up-facing to down-facing

Here is the code that I have gotten to work thus far, once the arrow is clicked the container expands/collapses:

function myFunction() {

var x = document.getElementById("this-container");

if (x.style.display === "none") {

x.style.display = "block";

} else {

x.style.display = "none";

}

}

I have tried other lines of javascript to get the arrow to change directions, with no luck. On thing that sort of work was using the javascript code to change the text upon click.. however doing so took away the expand/collapse function, and the text that it changed to did not pick up the set css styling.

Any help would be greatly appreciated!! :)

@epascarello Thanks for the tip, I think that actually maybe the easiest option. I did some research on how to use the toggle class, however, I think I maybe integrating it into my current code wrong? Here is the new code:

function myFunction() {

var x = document.getElementById("this-container");

if (x.style.display === "none")

{x.style.display = "block";x.toggleClass("drop-btn-do-down drop-btn-do-up");}

else {x.style.display = "none";}

}

The drop-down feature works, but the arrow direction does not change.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值