如何创建一个单击时包含新文本的按钮?

这里是令人难以置信的新手编码员。就像,除了 Tumblr 和 Neopets 教我如何阅读和进行基本的修补之外,完全是初学者。

我可能对这个项目已经咬牙切齿了,有人愿意帮忙吗?

单列中需要 5 个按钮,“单击”后文本会发生变化。我让它适用于 1,但在同一页面上添加 5 个按钮时,所有按钮都是随机的,我认为它们需要单独的 ID,但我不知道该怎么做。

    <!DOCTYPE html>


<html>

<head>

<style>

.button {

  background-color: #f0c640;

  border: none;

  color: #08365F;

  padding: 32px 32px;

  text-align: center;

  text-decoration: none;

  display: inline-block;

  font-size: 18px;

  margin: 4px 2px;

  cursor: pointer;

  font-family: 'Quattrocento Sans', sans-serif;



}

</style>

</head>

<body>

<center>


<input type="button" id= "bf3" class="button" value="BF₃" onclick="return change(this);" />


<script type="text/javascript">

function change( bf3 )

{

    if (bf3.value === "BF₃" )

        bf3.value = "boron trifluoride";

    else

        bf3.value = "BF₃";

}

</script>

</center>

</body>

</html>



<body>

<center>


<input type="button" id= "sf6" class="button" value="SF₆" onclick="return change(this);" />


<script type="text/javascript">

function change( sf6 )

{

    if ( sf6.value === "SF₆" )

        sf6.value = "sulfur hexafluoride";

    else

        sf6.value = "SF₆";

}

</script>

</center>

</body>

</html>


<body>

<center>


<input type="button" id="h2o" class="button" value="H₂O" onclick="return change(this);" />


<script type="text/javascript">

function change( h2o )

{

    if ( h2o.value === "H₂O" )

        h2o.value = "dihydrogen monoxide (aka water)";

    else

        h2o.value = "H₂O";

}

</script>

</center>

</body>


<body>

<center>


<input type="button" id="pcl5" class="button" value="PCl₅" onclick="return change(this);" />


<script type="text/javascript">

function change( pcl5 )

{

    if ( pcl5.value === "PCl₅" )

        pcl5.value = "phosphorus pentachloride";

    else

        pcl5.value = "PCl₅;

}

</script>

</center>

</body>

</html>


<body>

<center>


<input type="button" class="button" id="n2h4" value="N₂H₄" onclick="return change(this);" />


<script type="text/javascript">

function change( n2h4 )

{

    if ( n2h4.value === "N₂H₄" )

        n2h4.value = "dinitrogen tetrahydride";

    else

        n2h4.value = "N₂H₄;

}

</script>

</center>

</body>

</html>


POPMUISE
浏览 95回答 3
3回答

ITMISS

您不需要多个函数来实现这一目标。只需检查当前单击按钮的值并相应地设置该值即可。尝试以下方法:function change(currBtn){&nbsp;if(currBtn.value == 'BF₃' || currBtn.value == 'boron trifluoride'){&nbsp; &nbsp;if(currBtn.value == 'BF₃')&nbsp; &nbsp; &nbsp;currBtn.value = 'boron trifluoride';&nbsp; &nbsp;else&nbsp;&nbsp; &nbsp; &nbsp;currBtn.value = 'BF₃';&nbsp;}&nbsp;else if(currBtn.value == 'SF₆' || currBtn.value == 'sulfur hexafluoride'){&nbsp; &nbsp;if(currBtn.value == 'SF₆')&nbsp; &nbsp; &nbsp;currBtn.value = 'sulfur hexafluoride';&nbsp; &nbsp;else&nbsp; &nbsp; &nbsp;currBtn.value = 'SF₆';&nbsp;}&nbsp;else if(currBtn.value == 'H₂O' || currBtn.value == 'dihydrogen monoxide (aka water)'){&nbsp; &nbsp;if(currBtn.value == 'H₂O')&nbsp; &nbsp; &nbsp;currBtn.value = 'dihydrogen monoxide (aka water)';&nbsp; &nbsp;else&nbsp; &nbsp; &nbsp;currBtn.value = 'H₂O';&nbsp;}&nbsp;else if(currBtn.value == 'PCl₅' || currBtn.value == 'phosphorus pentachloride'){&nbsp; &nbsp;if(currBtn.value == 'PCl₅')&nbsp; &nbsp; &nbsp; currBtn.value = 'phosphorus pentachloride';&nbsp; &nbsp;else&nbsp; &nbsp; &nbsp;currBtn.value = 'PCl₅';&nbsp;}&nbsp;else if(currBtn.value == 'N₂H₄' || currBtn.value == 'dinitrogen tetrahydride'){&nbsp; &nbsp;if(currBtn.value == 'N₂H₄')&nbsp; &nbsp; &nbsp;currBtn.value = 'dinitrogen tetrahydride';&nbsp; &nbsp;else&nbsp; &nbsp; &nbsp;currBtn.value = 'N₂H₄'&nbsp;}}.button {&nbsp; background-color: #f0c640;&nbsp; border: none;&nbsp; color: #08365F;&nbsp; padding: 32px 32px;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; display: inline-block;&nbsp; font-size: 18px;&nbsp; margin: 4px 2px;&nbsp; cursor: pointer;&nbsp; font-family: 'Quattrocento Sans', sans-serif;}<center>&nbsp; <input type="button" id= "bf3" class="button" value="BF₃" onclick="change(this);" /></center><center>&nbsp; <input type="button" id= "sf6" class="button" value="SF₆" onclick="change(this);" /></center><center>&nbsp; <input type="button" id="h2o" class="button" value="H₂O" onclick="change(this);" /></center><center>&nbsp; <input type="button" id="pcl5" class="button" value="PCl₅" onclick="change(this);" /></center><center>&nbsp; <input type="button" class="button" id="n2h4" value="N₂H₄" onclick="change(this);" /></center>

GCT1015

为了提高可维护性,我会这样做:var container = document.getElementById('container');var molecules = [&nbsp; { formula: "BF₃",&nbsp; name: "boron trifluoride" },&nbsp; { formula: "SF₆",&nbsp; name: "sulfur hexafluoride" },&nbsp; { formula: "H₂O",&nbsp; name: "dihydrogen monoxide (aka water)" },&nbsp; { formula: "PCl₅", name: "phosphorus pentachloride" },&nbsp; { formula: "N₂H₄", name: "dinitrogen tetrahydride" }];molecules.forEach(function(m) {&nbsp; var showName = false;&nbsp; var btn = document.createElement('input');&nbsp; btn.type = 'button';&nbsp; btn.className = 'button';&nbsp; btn.value = m.formula;&nbsp; btn.addEventListener('click', function() {&nbsp; &nbsp; showName = !showName;&nbsp; &nbsp; btn.value = showName ? m.name : m.formula;&nbsp; });&nbsp; container.appendChild(btn);&nbsp; container.appendChild(document.createElement('br'));});#container { text-align: center; }.button {&nbsp; background-color: #f0c640;&nbsp; border: none;&nbsp; color: #08365F;&nbsp; padding: 32px 32px;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; display: inline-block;&nbsp; font-size: 18px;&nbsp; margin: 4px 2px;&nbsp; cursor: pointer;&nbsp; font-family: 'Quattrocento Sans', sans-serif;}<div id="container"></div>

莫回无

我的方式...const buttonList =&nbsp;&nbsp; [ [ 'BF₃',&nbsp; 'boron trifluoride'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp; , [ 'SF₆',&nbsp; 'sulfur hexafluoride'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp;&nbsp; , [ 'H₂O',&nbsp; 'dihydrogen monoxide (aka water)' ]&nbsp; , [ 'PCl₅', 'phosphorus pentachloride'&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp;&nbsp;&nbsp; , [ 'N₂H₄', 'dinitrogen tetrahydride'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp; ];buttonList.forEach(bt=>&nbsp; {&nbsp; let newbt = document.createElement('button')&nbsp; &nbsp; , timOut = null;&nbsp; newbt.className = 'button'&nbsp; newbt.textContent = bt[0]&nbsp; document.documentElement.appendChild( newbt )&nbsp; newbt.onclick=()=>{&nbsp; &nbsp; newbt.textContent = bt[1]&nbsp; &nbsp; clearTimeout(timOut)&nbsp; &nbsp; timOut = setTimeout(()=>{ newbt.textContent = bt[0] }, 1500)&nbsp; &nbsp; }&nbsp; })/* orbuttonList.forEach(bt=>&nbsp; {&nbsp; let newbt = document.createElement('button')&nbsp; &nbsp; , LibNum = 0&nbsp; newbt.className = 'button'&nbsp; newbt.textContent =&nbsp; bt[0]&nbsp; newbt.onclick=()=>{ LibNum = ++LibNum %2; newbt.textContent = bt[LibNum]&nbsp; }&nbsp; document.documentElement.appendChild( newbt )&nbsp; })*/.button {&nbsp; font-family: 'Quattrocento Sans', sans-serif;&nbsp; font-size: 18px;&nbsp; display: block;&nbsp; margin: .2em auto;&nbsp; padding: 1em;&nbsp; color: #08365F;&nbsp; background-color: #f0c640;&nbsp; border: none;&nbsp; cursor: pointer;&nbsp; text-align: center;&nbsp; min-width: 5em;}我是否还花时间更正您的代码来帮助您?<!DOCTYPE html><html><head>&nbsp; <meta charset='UTF-8'>&nbsp; <title> sample code </title>&nbsp; <style>&nbsp; &nbsp; .button {&nbsp; &nbsp; &nbsp; background-color: #f0c640;&nbsp; &nbsp; &nbsp; border: none;&nbsp; &nbsp; &nbsp; color: #08365F;&nbsp; &nbsp; &nbsp; padding: 32px;&nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; /* must be block to be centered */&nbsp; &nbsp; &nbsp; font-size: 18px;&nbsp; &nbsp; &nbsp; margin: 4px auto;&nbsp; /* this one replace <center> tag (obsolete)&nbsp; */&nbsp; &nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; &nbsp; font-family: 'Quattrocento Sans', sans-serif;&nbsp; &nbsp; }&nbsp; </style></head><body>&nbsp; &nbsp; <input type="button" id="bf3"&nbsp; class="button" value="BF₃"&nbsp; onclick="xchange(this);" />&nbsp; &nbsp; <input type="button" id="sf6"&nbsp; class="button" value="SF₆"&nbsp; onclick="xchange(this);" />&nbsp; &nbsp; <input type="button" id="h2o"&nbsp; class="button" value="H₂O"&nbsp; onclick="xchange(this);" />&nbsp; &nbsp; <input type="button" id="pcl5" class="button" value="PCl₅" onclick="xchange(this);" />&nbsp; &nbsp; <input type="button" id="n2h4" class="button" value="N₂H₄" onclick="xchange(this);" />&nbsp; <script>&nbsp; &nbsp; function xchange( btn )&nbsp; &nbsp; {&nbsp; &nbsp; switch (btn.id) {&nbsp; &nbsp; &nbsp; case 'bf3':&nbsp; btn.value = (btn.value==='BF₃')&nbsp; ? 'boron trifluoride' : 'BF₃'; break;&nbsp; &nbsp; &nbsp; case 'sf6':&nbsp; btn.value = (btn.value==='SF₆')&nbsp; ? 'sulfur hexafluoride' : 'SF₆'; break;&nbsp; &nbsp; &nbsp; case 'h2o':&nbsp; btn.value = (btn.value==='H₂O')&nbsp; ? 'dihydrogen monoxide (aka water)' : 'H₂O'; break;&nbsp; &nbsp; &nbsp; case 'pcl5': btn.value = (btn.value==='PCl₅') ? 'phosphorus pentachloride' : 'PCl₅'; break;&nbsp; &nbsp; &nbsp; case 'n2h4': btn.value = (btn.value==='N₂H₄') ? 'dinitrogen tetrahydride' : 'N₂H₄'; break;&nbsp; &nbsp; } }&nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5