如何在JavaScript代码中添加HTML按钮?

huangapple go评论105阅读模式
英文:

How to add a HTML button in JavaScript code?

问题

我要翻译的内容:

如何在JavaScript中调用函数时,只在HTML中添加一个按钮?我有一个函数,当您在文本框中写入一些文本并单击按钮时,该文本将显示在HTML上,但我想在文本旁边添加一个弹出的按钮。我该如何在JavaScript代码中编写这部分?

英文:

How do I add a button that only shows up in HTML when a function is called in JavaScript? I have a function where when you write some text in a text-box and click on a button - that said text was written on the HTML, but I want to add a button next to the text that pops up. How do I write that in the JavaScript code?

答案1

得分: 2

你是指这样的吗?

  1. <script>
  2. var btn = document.createElement("BUTTON");
  3. btn.innerHTML = "New Button";
  4. function appendButton(){
  5. document.getElementById("container").appendChild(btn);
  6. }
  7. </script>
  8. <div id='container'>
  9. <button id='button1' onclick="appendButton()">Click Me</button>
  10. </div>
英文:

Do you mean something like this?

  1. &lt;script&gt;
  2. var btn = document.createElement(&quot;BUTTON&quot;);
  3. btn.innerHTML = &quot;New Button&quot;;
  4. function appendButton(){
  5. document.getElementById(&quot;container&quot;).appendChild(btn);
  6. }
  7. &lt;/script&gt;
  8. &lt;div id=&#39;container&#39;&gt;
  9. &lt;button id=&#39;button1&#39; onclick=&quot;appendButton()&quot;&gt;Click Me&lt;/button&gt;
  10. &lt;/div&gt;

答案2

得分: -1

以下是翻译好的部分:

在JavaScript中添加HTML按钮有点多余,因为你可以直接在HTML中编写它。

你可以使用.append() JavaScript函数与Jquery一起使用:

  1. $(function(){
  2. $('button').on('click',function(){
  3. var r= $('<input type="button" value="new button"/>');
  4. $("body").append(r);
  5. });
  6. });
英文:

It is kind of useless to add a HTML button in Javascript when you can just write it in HTML.

You can use the .append() Javascript function with Jquery:

  1. $(function(){
  2. $(&#39;button&#39;).on(&#39;click&#39;,function(){
  3. var r= $(&#39;&lt;input type=&quot;button&quot; value=&quot;new button&quot;/&gt;&#39;);
  4. $(&quot;body&quot;).append(r);
  5. });
  6. });

答案3

得分: -1

如何钓鱼:反复使用一个函数,这样你就不必一次又一次地重新弄清楚。我为我的网络平台保留了一组大量可重用的函数。

有一个注意事项:我编写严格的代码,所以如果只有一个元素,只需在任何地方添加 xmlns=&quot;http://www.w3.org/1999/xhtml&quot;属性/值即可。如果你需要多个元素,你将需要一个包含所有具有 xmlns=&quot;http://www.w3.org/1999/xhtml&quot; XML命名空间的子元素的单个包含父元素。我在这方面的效率非常高,因为它是严格的代码。

在本帖底部需要两个先决函数。

  1. xml_add('before', id_('element_after'), '&lt;input type=&quot;button&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;');
  2. xml_add('after', id_('element_before'), '&lt;input type=&quot;text&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;');
  3. xml_add('inside', id_('element_parent'), '&lt;input type=&quot;text&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;');

添加多个元素(命名空间只需要在父元素上):

  1. xml_add('inside', id_('element_parent'), '&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;input type=&quot;text&quot; /&gt;&lt;input type=&quot;button&quot; /&gt;&lt;/div&gt;');

动态可重用代码:

  1. function id_(id) {return (document.getElementById(id)) ? document.getElementById(id) : false;}
  2. function xml_add(pos, e, xml)
  3. {
  4. e = (typeof e == 'string' && id_(e)) ? id_(e) : e;
  5. if (e.nodeName)
  6. {
  7. if (pos=='after') {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true),e.nextSibling);}
  8. else if (pos=='before') {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true),e);}
  9. else if (pos=='inside') {e.appendChild(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true));}
  10. else if (pos=='replace') {e.parentNode.replaceChild(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true),e);}
  11. //添加片段并将其返回。
  12. }
  13. }
英文:

How to fish: use a function over and over again so you don't have to keep figuring this out. I keep a large set of highly reusable functions for my web platform.

There is one caveat: I code strict so if it's a single element just add the xmlns=&quot;http://www.w3.org/1999/xhtml&quot; attribute/value any where. If you need multiple elements you'll need a single containing parent that contains all the child elements that has the xmlns=&quot;http://www.w3.org/1999/xhtml&quot; XML namespace. I run circles in efficiency with this stuff because it's strict code.

There are two prerequisite functions needed at the bottom of this post.

  1. xml_add(&#39;before&#39;, id_(&#39;element_after&#39;), &#39;&lt;input type=&quot;button&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;&#39;);
  2. xml_add(&#39;after&#39;, id_(&#39;element_before&#39;), &#39;&lt;input type=&quot;text&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;&#39;);
  3. xml_add(&#39;inside&#39;, id_(&#39;element_parent&#39;), &#39;&lt;input type=&quot;text&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;&#39;);

Add multiple elements (namespace only needs to be on the parent element):

  1. xml_add(&#39;inside&#39;, id_(&#39;element_parent&#39;), &#39;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;input type=&quot;text&quot; /&gt;&lt;input type=&quot;button&quot; /&gt;&lt;/div&gt;&#39;);

Dynamic reusable code:

  1. function id_(id) {return (document.getElementById(id)) ? document.getElementById(id) : false;}
  2. function xml_add(pos, e, xml)
  3. {
  4. e = (typeof e == &#39;string&#39; &amp;&amp; id_(e)) ? id_(e) : e;
  5. if (e.nodeName)
  6. {
  7. if (pos==&#39;after&#39;) {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true),e.nextSibling);}
  8. else if (pos==&#39;before&#39;) {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true),e);}
  9. else if (pos==&#39;inside&#39;) {e.appendChild(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true));}
  10. else if (pos==&#39;replace&#39;) {e.parentNode.replaceChild(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true),e);}
  11. //Add fragment and have it returned.
  12. }
  13. }

huangapple
  • 本文由 发表于 2020年1月3日 21:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定