简体   繁体   English

如果名称较长,如何使可单击按钮保持在原位

[英]How to make clickable buttons stay in place if they have long names

I'm creating few buttons, and one of them gets replaced by a long named button. 我正在创建几个按钮,其中一个被一个长按钮取代。 Basically when I click click me button, a button named banana , gets replaced by another button named order apple, banana and orange . 基本上,当我单击“ click me按钮时,一个名为“ banana的按钮将替换为另一个名为“ order apple, banana and orange按钮。 The problem I'm running into is click me button moves away from my cursor position on the screen(on to the left side). 我遇到的问题是“ click me按钮从屏幕上的光标位置移开(在左侧)。 I want the click me button and apple button to stay in place even after clicking click me . 我希望“ click me按钮和“ apple按钮即使在单击“ click me之后仍保持在原位。 Note that, on js side, I need to remove banana button off the DOM for future styling purposes. 请注意,在JS端,我需要从DOM上删除banana按钮,以备将来使用。 How do I go about this? 我该怎么办?

 var allDivsButton = document.createElement("div"); allDivsButton.setAttribute("class", "allbuttons"); var buttonOne = document.createElement("button"); buttonOne.setAttribute("type", "button submit"); buttonOne.setAttribute("class", "buttons"); buttonOne.innerHTML = "click me" allDivsButton.appendChild(buttonOne) var buttonTwo = document.createElement("button"); buttonTwo.setAttribute("type", "button"); buttonTwo.setAttribute("class", "buttons"); allDivsButton.appendChild(buttonTwo); buttonTwo.innerHTML = "apple" var buttonThree = document.createElement("button"); buttonThree.setAttribute("type", "button"); buttonThree.setAttribute("class", "buttons"); allDivsButton.appendChild(buttonThree); buttonThree.innerHTML = "banana" var buttonFour = document.createElement("button"); buttonFour.setAttribute("type", "button"); buttonFour.setAttribute("class", "buttons"); buttonFour.setAttribute("id", "button4"); buttonFour.innerHTML = " order apple, banana and orange " document.body.appendChild(allDivsButton); buttonOne.addEventListener('click', function(){ buttonThree.remove(); allDivsButton.appendChild(buttonFour); }) 
 .allbuttons{ background-color: red; width: 100%; height: 50px; text-align: center; } .buttons{ padding: 10px; margin: 10px; } 

edit: 编辑:

I mentioned remove() only because I don't want style-display = "none" option on it. 我之所以提到remove()只是因为我不想在其上使用style-display =“ none”选项。

 var allDivsButton = document.createElement("div"); allDivsButton.setAttribute("class", "allbuttons"); var buttonOne = document.createElement("button"); buttonOne.setAttribute("type", "button submit"); buttonOne.setAttribute("class", "buttons"); buttonOne.innerHTML = "click me" allDivsButton.appendChild(buttonOne) var buttonTwo = document.createElement("button"); buttonTwo.setAttribute("type", "button"); buttonTwo.setAttribute("class", "buttons"); allDivsButton.appendChild(buttonTwo); buttonTwo.innerHTML = "apple" var buttonThree = document.createElement("button"); buttonThree.setAttribute("type", "button"); buttonThree.setAttribute("class", "buttons"); allDivsButton.appendChild(buttonThree); buttonThree.innerHTML = "banana" var buttonFour = document.createElement("button"); buttonFour.setAttribute("type", "button"); buttonFour.setAttribute("class", "buttons"); buttonFour.setAttribute("id", "button4"); buttonFour.innerHTML = " order apple, banana and orange " document.body.appendChild(allDivsButton); buttonOne.addEventListener('click', function(){ buttonThree.remove(); allDivsButton.appendChild(buttonFour); }); var cloneall = allDivsButton.cloneNode(true); document.body.appendChild(cloneall); cloneall.setAttribute("class", "allbuttonsclone"); var clonebutton = buttonFour.cloneNode(true); clonebutton.setAttribute("class", "buttons buttonclone hidden"); cloneall.appendChild(clonebutton); cloneall.children[0].addEventListener('click', function(){ cloneall.children[2].setAttribute("class", "buttons hidden"); clonebutton.setAttribute("class", "buttons buttonclone"); }); 
 .allbuttons { background-color: red; width: 100%; height: 50px; text-align: left; } .allbuttonsclone { background-color: orange; width: 100%; height: 50px; text-align: center; } .buttons { padding: 10px; margin: 10px; } .buttonclone { position: absolute; width: 220px; top: 58px; left: 50%; margin-left: 50px; } .hidden { opacity: 0; pointer-events: none; } 

I think you are trying to use the jQuery .remove() on buttonThree. 我认为您正在尝试在buttonThree上使用jQuery .remove()。 In javaScript you could use something like: 在javaScript中,您可以使用如下代码:

allDivsButton.removeChild(buttonThree);

You also need to look at the css. 您还需要查看CSS。 The class .allbuttons has the attribute of text-align: center; .allbuttons类具有text-align属性:center; which is causing the behavior that is causing the buttons to 'move'. 这导致了导致按钮“移动”的行为。 You could have the buttons wrapped in an additional div that has a specific width so the buttons don't move. 您可以将按钮包装在具有特定宽度的附加div中,以使按钮不会移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM