如何编程一个按钮来使用javascript更改样式表
请注意,我没有使用类。我还没有找到这个具体问题的答案 使用javascript,我如何编程一个按钮以在每次单击按钮时更改样式表 我尝试了不同的if、else if和else,但当我尝试它们时,它会破坏代码(即,如果红色,它会将颜色更改为蓝色,但不会再次更改)。如何编程一个按钮来使用javascript更改样式表,javascript,html,css,Javascript,Html,Css,请注意,我没有使用类。我还没有找到这个具体问题的答案 使用javascript,我如何编程一个按钮以在每次单击按钮时更改样式表 我尝试了不同的if、else if和else,但当我尝试它们时,它会破坏代码(即,如果红色,它会将颜色更改为蓝色,但不会再次更改)。 它可以使用两个按钮,但每次单击一个按钮时都要更改它,这似乎让我难以理解。我得到了反馈,并设置了第二个按钮来更改它 这适用于2个按钮: <!DOCTYPE html> <html> <head>
它可以使用两个按钮,但每次单击一个按钮时都要更改它,这似乎让我难以理解。我得到了反馈,并设置了第二个按钮来更改它 这适用于2个按钮:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>"Your Title Here"</title>
<link id="um" rel="stylesheet" href="stylesheet1.css">
<style>
</style>
</head>
<body>
<p>booga</p>
<button id="x" onclick="myFunction()">blue</button>
<button id="x1" onclick="myFunction1()">red</button>
<script>
function myFunction() {
if (document.getElementById("um").href = "stylesheet1.css"){
document.getElementById("um").href = "stylesheet2.css"}
}
function myFunction1() {
if (document.getElementById("um").href = "stylesheet2.css"){
document.getElementById("um").href = "stylesheet1.css"}
}
</script>
</body>
确保使用==而不是=进行比较
if (document.getElementById("um").href == "stylesheet1.css")
etc确保使用==而不是=进行比较
if (document.getElementById("um").href == "stylesheet1.css")
etc试试这个:
<button id="x" onclick="myFunction()">Change</button>
<script>
function myFunction() {
var link = document.getElementById("um");
var segments = link.href.split('/');
var currentStyle = segments[segments.length - 1];
var style = (currentStyle == 'stylesheet1.css') ? 'stylesheet2'
: 'stylesheet1';
document.getElementById("um").href = style + ".css"
}
</script>
更改
函数myFunction(){
var link=document.getElementById(“um”);
var segments=link.href.split('/');
var currentStyle=段[segments.length-1];
变量样式=(currentStyle=='stylesheet1.css')?'stylesheet2'
:“样式表1”;
document.getElementById(“um”).href=style+“.css”
}
试试这个:
<button id="x" onclick="myFunction()">Change</button>
<script>
function myFunction() {
var link = document.getElementById("um");
var segments = link.href.split('/');
var currentStyle = segments[segments.length - 1];
var style = (currentStyle == 'stylesheet1.css') ? 'stylesheet2'
: 'stylesheet1';
document.getElementById("um").href = style + ".css"
}
</script>
更改
函数myFunction(){
var link=document.getElementById(“um”);
var segments=link.href.split('/');
var currentStyle=段[segments.length-1];
变量样式=(currentStyle=='stylesheet1.css')?'stylesheet2'
:“样式表1”;
document.getElementById(“um”).href=style+“.css”
}
“你的头衔在这里”
布加
交换
函数myFunction(id、a、b){
var el=document.getElementById(id);
var-hrefStr;
如果(~el.href.indexOf(a)){
hrefStr=el.href.replace(a,b);
el.href=hrefStr;
}否则{
hrefStr=el.href.replace(b,a);
el.href=hrefStr;
}
}
“你的头衔在这里”
布加
交换
函数myFunction(id、a、b){
var el=document.getElementById(id);
var-hrefStr;
如果(~el.href.indexOf(a)){
hrefStr=el.href.replace(a,b);
el.href=hrefStr;
}否则{
hrefStr=el.href.replace(b,a);
el.href=hrefStr;
}
}
@Typo:Mmm,不,不是。当有人总是说“哦,那是重复的!打倒你!”时,一个新程序员如何在这个网站上获得任何代表性?Bugiroff耐心点,事情需要时间。你能帮我吗vote@Typo:嗯,不,不是。当有人总是说“哦,那是个复制品!打倒你!”时,一个新的程序员怎么能在这个网站上获得任何代表呢?@Bugiroff耐心点,事情需要时间。你有没有办法解决我的问题。。。href属性将是绝对url,与属性值不同,因此更像是函数myFunction(){if(document.getElementById(“um”).href==“stylesheet1.css”){document.getElementById(“um”).href=“stylesheet2.css”};else{document.getElementById(“um”).href=“stylesheet1.css”}来检查样式表名称,如果(document.getElementById(“um”).href.indexOf(“stylesheet1”)>-1)不能解决整个问题,您应该能够使用它。。。href属性将是绝对url,与属性值不同,因此更像是函数myFunction(){if(document.getElementById(“um”).href==“stylesheet1.css”){document.getElementById(“um”).href=“stylesheet2.css”};else{document.getElementById(“um”).href=“stylesheet1.css”}来检查样式表名称,您应该能够在my case console.log(document.getElementById(“um”).href.indexOf(“样式表1”)>-1)中使用if(document.getElementById(“um”).href)返回file:///C:/Users/aespiritu/Desktop/aloha/stylesheet1.cssIn my case console.log(document.getElementById(“um”).href)返回file:///C:/Users/aespiritu/Desktop/aloha/stylesheet1.css