为什么 onclick 函数在下面的 Javascript 片段中不起作用?
Why onclick function isn't working in Javascript snippet below?
function toggleHide(){
let btn = document.getElementById(`btn`);
let para = document.getElementById(`para`);
if(para.style.display != 'none'){
para.style.diplay = 'none';
}
else{
para.style.display = 'block';
}
}
<p id="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, veniam quia natus animi quos reprehenderit
voluptates deleniti. Nesciunt fugit adipisci veritatis asperiores tempora voluptatibus est illo nulla tenetur ex</p>
<button id="btn" onclick="toggleHide()"> Show / Hide </button>
代码的工作是通过显示和隐藏来切换段落,即使 JS 控制台也无法识别这里出了什么问题!
你把第二个显示拼错了。
function toggleHide(){
let btn = document.getElementById(`btn`);
let para = document.getElementById(`para`);
if(para.style.display != 'none'){
para.style.display = 'none';
}
else{
para.style.display = 'block';
}
}
<p id="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, veniam quia natus animi quos reprehenderit
voluptates deleniti. Nesciunt fugit adipisci veritatis asperiores tempora voluptatibus est illo nulla tenetur ex</p>
<button id="btn" onclick="toggleHide()"> Show / Hide </button>
function toggleHide(){
let btn = document.getElementById(`btn`);
let para = document.getElementById(`para`);
if(para.style.display != 'none'){
para.style.diplay = 'none';
}
else{
para.style.display = 'block';
}
}
<p id="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, veniam quia natus animi quos reprehenderit
voluptates deleniti. Nesciunt fugit adipisci veritatis asperiores tempora voluptatibus est illo nulla tenetur ex</p>
<button id="btn" onclick="toggleHide()"> Show / Hide </button>
代码的工作是通过显示和隐藏来切换段落,即使 JS 控制台也无法识别这里出了什么问题!
你把第二个显示拼错了。
function toggleHide(){
let btn = document.getElementById(`btn`);
let para = document.getElementById(`para`);
if(para.style.display != 'none'){
para.style.display = 'none';
}
else{
para.style.display = 'block';
}
}
<p id="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, veniam quia natus animi quos reprehenderit
voluptates deleniti. Nesciunt fugit adipisci veritatis asperiores tempora voluptatibus est illo nulla tenetur ex</p>
<button id="btn" onclick="toggleHide()"> Show / Hide </button>