gpt4 book ai didi

jquery - 问题定位要素

转载 作者:行者123 更新时间:2023-11-28 05:33:37 25 4
gpt4 key购买 nike

我在将这些元素放置在我的页面上时遇到问题,它应该非常简单,但我不明白为什么它们会这样。

我试图让按钮沿左侧对齐,相应的随机生成的数字位于它们的右侧。但是每当我尝试移动单个元素(无论我使用什么选择器)时,它们都会同时移动,这是我的代码。

<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" type="text/css" href="Main.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>



<button id="DemoButton" class="button button5" onclick="myFunction()">D20</button>
<p id="demo"></p>


<script>
function myFunction() {
var x = Math.floor((Math.random() * 20) + 1);
document.getElementById("demo").innerHTML = x;
}
</script>


<button id="DemoButton2" class="button button5" onclick="myFunction1()">D12</button>

<p id="demo1"></p>

<script>

function myFunction1() {
var x = Math.floor((Math.random() * 12) + 1);
document.getElementById("demo1").innerHTML = x;
}

</script>

<button id="DemoButton3" class="button button5" onclick="myFunction2()">D8</button>

<p id="demo2"></p>

<script>

function myFunction2() {
var x = Math.floor((Math.random() * 8) + 1);
document.getElementById("demo2").innerHTML = x;
}

</script>

<button class="button button5 demobutton4" onclick="myFunction4()">D6</button>

<p id="demo4"></p>

<script>

function myFunction4() {
var x = Math.floor((Math.random() * 6) + 1);
document.getElementById("demo4").innerHTML = x;
}

</script>

<button id="DemoButton4" class="button button5" onclick="myFunction3()">D4</button>

<p id="demo3"></p>

<script>

function myFunction3() {
var x = Math.floor((Math.random() * 4) + 1);
document.getElementById("demo3").innerHTML = x;
}

</script>



</body>
</html>

和 CSS

#demo {
display:inline;
}

#demo1 {
display:inline;

}

#demo2 {
display:inline;

}

#demo4 {
display:inline;

}

#demo3 {
display:inline;

}


.demobutton4 {
margin-top: 1px;
}

.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
cursor: pointer;
}

.button5 {
background-color: white;
color: black;
border: 2px solid #555555;
}

.button5:hover {
background-color: #555555;
color: white;
}

感谢我能得到的任何帮助。

最佳答案

这是更新后的代码,带有中断但没有内联属性,并且可以正常工作。

<head>
<link rel="stylesheet" type="text/css" href="Main.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>



<button id="DemoButton" class="button button5" onclick="myFunction()">D20</button>
<p id="demo"></p>

<br/>
<script>
function myFunction() {
var x = Math.floor((Math.random() * 20) + 1);
document.getElementById("demo").innerHTML = x;
}
</script>


<button id="DemoButton2" class="button button5" onclick="myFunction1()">D12</button>

<p id="demo1"></p>
<br/>
<script>

function myFunction1() {
var x = Math.floor((Math.random() * 12) + 1);
document.getElementById("demo1").innerHTML = x;
}

</script>

<button id="DemoButton3" class="button button5" onclick="myFunction2()">D8</button>

<p id="demo2"></p>
<br />
<script>

function myFunction2() {
var x = Math.floor((Math.random() * 8) + 1);
document.getElementById("demo2").innerHTML = x;
}

</script>

<button class="button button5 demobutton4" onclick="myFunction4()">D6</button>

<p id="demo4"></p>
<br/>
<script>

function myFunction4() {
var x = Math.floor((Math.random() * 6) + 1);
document.getElementById("demo4").innerHTML = x;
}

</script>

<button id="DemoButton4" class="button button5" onclick="myFunction3()">D4</button>

<p id="demo3"></p>
<br/>
<script>

function myFunction3() {
var x = Math.floor((Math.random() * 4) + 1);
document.getElementById("demo3").innerHTML = x;
}

</script>



</body>
</html>

和CSS

#demo {
display:inline;
}

#demo1 {
display:inline;

}

#demo2 {
display:inline;

}

#demo4 {
display:inline;

}

#demo3 {
display:inline;

}


.demobutton4 {
margin-top: 1px;
}

.button {

background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;


font-size: 16px;
margin: 4px 2px;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
cursor: pointer;
}

.button5 {

background-color: white;
color: black;
border: 2px solid #555555;
}

.button5:hover {
background-color: #555555;
color: white;
}

关于jquery - 问题定位要素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38360451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com