我正在用 javascript 开发 duolingo 类型句子练习。我已经实现了,但还需要改进

我使用以下代码来开发句子语法练习。当我单击按钮时,应该保持订单。我希望单击按钮时它应该隐藏,但单击顶部按钮后再次显示。

如果有空格,则将句子移至左侧。如果再次单击单词,也会再次显示按钮。是否应该只使用在顶部显示的按钮也在底部显示?


<html>

<head>

<title>

</title> 

</head>


<body>


<div id="sen">I am learning JavaScript by developing a simple project.</div>

<br>

<div id="dash"></div>

<br>

<div id="container"></div>

<div id="val"></div>


<script>

var sen = document.getElementById("sen").innerHTML;

var senTrim = sen.trim();

var senArr = senTrim.split(/\s+/);


var dashElement = "";

for(i=0;i<senArr.length;i++)

{

//alert(senArr[i]);

    dashElement += "<div onclick='funDiv(this.id);' style='display: inline'" + "id = dashid" + i + ">"    + '__ '  + '</div>';


}


var dash = document.getElementById("dash");

dash.innerHTML = dashElement;


//var dashID = document.getElementById("dashid0").innerHTML;

//var dash1 = document.getElementById("val");

//dash1.innerHTML= dashID;


var htmlElements = "";

for (var i = 0; i < senArr.length; i++) {

   htmlElements += "<button onclick='fun(this.id);' id = 'btn" + i + "'>" + senArr[i] + '</button>';

}

var container = document.getElementById("container");

container.innerHTML = htmlElements;


var ii = 0;

function funDiv(clicked){

//alert(clicked);

var inText = document.getElementById(clicked).innerHTML;

document.getElementById(clicked).innerHTML = " __ " ; 

ii--;

}


function fun(clicked){

//alert(clicked);

document.getElementById(clicked).style.display = "none";  

document.getElementById("dashid" + ii).innerHTML = document.getElementById(clicked).innerHTML + " ";

//document.getElementById(clicked).remove(); 

ii++;

}


</script>


</script>

</body>

</html>

https://img1.sycdn.imooc.com/6576d30800017f7a05880265.jpg

杨魅力
浏览 128回答 2
2回答

幕布斯7119047

像这样的事情怎么样?<html><body>&nbsp; <div id="sen">I am learning JavaScript by developing a simple project.</div>&nbsp; <br>&nbsp; <div id="dash"></div>&nbsp; <br>&nbsp; <div id="container"></div>&nbsp; <div id="val"></div>&nbsp; <script>&nbsp; &nbsp; var sen = document.getElementById("sen").innerHTML;&nbsp; &nbsp; var senTrim = sen.trim();&nbsp; &nbsp; var senArr = senTrim.split(/\s+/);&nbsp; &nbsp; var dashElement = "";&nbsp; &nbsp; for (var i = 0; i < senArr.length; i++) {&nbsp; &nbsp; &nbsp; dashElement += `<div onclick='dashClick(this.id);' style='display: inline' id=dash${i}> __ </div>`;&nbsp; &nbsp; }&nbsp; &nbsp; var dash = document.getElementById("dash");&nbsp; &nbsp; dash.innerHTML = dashElement;&nbsp; &nbsp; var htmlElements = "";&nbsp; &nbsp; for (var i = 0; i < senArr.length; i++) {&nbsp; &nbsp; &nbsp; htmlElements += "<button onclick='btnClick(this.id);' id = 'btn" + i + "'>" + senArr[i] + '</button>';&nbsp; &nbsp; }&nbsp; &nbsp; var container = document.getElementById("container");&nbsp; &nbsp; container.innerHTML = htmlElements;&nbsp; &nbsp; var picked = 0;&nbsp; &nbsp; function dashClick(clicked) {&nbsp; &nbsp; &nbsp; const dash = document.getElementById(clicked);&nbsp; &nbsp; &nbsp; dash.innerHTML = " __ ";&nbsp; &nbsp; &nbsp; const btn = document.getElementById(`btn${dash.btnId}`);&nbsp; &nbsp; &nbsp; btn.style.display = "inline";&nbsp; &nbsp; &nbsp; picked--;&nbsp; &nbsp; }&nbsp; &nbsp; function btnClick(clicked) {&nbsp; &nbsp; &nbsp; var btnId = clicked.replace('btn', '');&nbsp; &nbsp; &nbsp; document.getElementById(clicked).style.display = "none";&nbsp; &nbsp; &nbsp; const dash = document.getElementById("dash" + picked)&nbsp; &nbsp; &nbsp; dash.innerHTML = document.getElementById(clicked).innerHTML + " ";&nbsp; &nbsp; &nbsp; dash.btnId = btnId;&nbsp; &nbsp; &nbsp; picked++;&nbsp; &nbsp; }&nbsp; </script></body></html>

繁星coding

我使用JavaScript的appendChild和remove函数实现了它。<html><body>&nbsp; <div id="sen">I am learning JavaScript by developing a simple project.</div>&nbsp; <br>&nbsp; <div id="dash"></div>&nbsp; <br>&nbsp; <div id="container"></div>&nbsp;&nbsp;&nbsp; <script>&nbsp; &nbsp; var sen = document.getElementById("sen").innerHTML;&nbsp; &nbsp; var senTrim = sen.trim();&nbsp; &nbsp; var senArr = senTrim.split(/\s+/);&nbsp; &nbsp; var dashElement = "";&nbsp; &nbsp; var btnElements = "";&nbsp; &nbsp; for (var i = 0; i < senArr.length; i++) {&nbsp; &nbsp; &nbsp; btnElements += "<button onclick='btnClick(this.id);' id = 'btn" + i + "'> " + senArr[i] + ' </button>';&nbsp; &nbsp; }&nbsp; &nbsp; var container = document.getElementById("container");&nbsp; &nbsp; container.innerHTML = btnElements;&nbsp; &nbsp; var picked = 0;&nbsp; &nbsp; function dashClick(clicked) {&nbsp; &nbsp; &nbsp; &nbsp; //console.log(clicked);&nbsp; &nbsp; &nbsp; &nbsp; var buttons = document.getElementsByTagName('button');&nbsp; &nbsp; &nbsp; &nbsp; var dash = document.getElementById("dash");&nbsp; &nbsp; &nbsp; &nbsp; dashChild = dash.childNodes;&nbsp; &nbsp; &nbsp; &nbsp; console.log(document.getElementById(clicked).innerText);&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<senArr.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(document.getElementById(clicked).innerText.trim() == buttons[i].innerText.trim()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("Match");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[i].style.opacity = "1";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons[i].style.pointerEvents = "auto";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(clicked).remove(); // remove clicked text&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; // Button click&nbsp; &nbsp; function btnClick(clicked) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var dashElement = document.createElement("div");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var text = document.getElementById(clicked).innerText;&nbsp; &nbsp; &nbsp; &nbsp; dashElement.style.display = "inline";&nbsp; &nbsp; &nbsp; &nbsp; dashElement.innerHTML = "<div style='display: inline' onclick='dashClick(this.id);' id=" + picked +"> " + text + " </div>"; // add text at top of button&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("dash").appendChild(dashElement);&nbsp; &nbsp; &nbsp; &nbsp; picked++;&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(clicked).style.opacity = "0"; //hide button that has been clicked&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(clicked).style.pointerEvents = "none";&nbsp; &nbsp; }&nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5