1 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
您無(wú)法重用 onClick 函數(shù)(制作“5%”html)的原因是您已經(jīng)用removeHTML 函數(shù)覆蓋了該函數(shù)。我使用一個(gè)事件函數(shù)來(lái)寫入和刪除內(nèi)部 html 文本。有很多方法可以實(shí)現(xiàn)你想要的。希望你嘗試自己的方式。
// Create New Element
let html = document.createElement('p');
html.className = 'skills';
html.id = 'html-skills';
html.setAttribute('title', 'HTML5 Proficiency');
html.appendChild(document.createTextNode(''));
html.style.setProperty('font-size', '16px')
document.getElementById('htmlJS').appendChild(html)
// Grab Old Element
const oldHTML = document.getElementById('html');
// On Click function to add element
oldHTML.addEventListener('click', onClick);
function onClick(e) {
if (html.innerHTML === '') {
html.innerHTML = '5%'
} else {
html.innerHTML = ''
}
}
// On click function to remove element
//Whatever you named this htmlJS id it still has onCLick function.
const htmlJS = document.getElementById('htmlJS')
html.addEventListener('click', removeHTML);
function removeHTML(e) {
let newHTML = document.getElementById("htmlJS");
let htmlNested = document.getElementById("htmlJS").lastChild;
}
<script
src="https://kit.fontawesome.com/d3d2bbf2ae.js"
crossorigin="anonymous"
></script>
<div class="skills-list__1 skills-icons" id="skills1">
<i class="fab fa-html5 html" id="html" title="HTML5"><div id="htmlJS"></div></i>
</div>
添加回答
舉報(bào)