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

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

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

你是指这样的吗?

<script>
  var btn = document.createElement("BUTTON");
  btn.innerHTML = "New Button";

  function appendButton(){
    document.getElementById("container").appendChild(btn);
  }
</script>
<div id='container'>
  <button id='button1' onclick="appendButton()">Click Me</button>
</div>
英文:

Do you mean something like this?

&lt;script&gt;
  var btn = document.createElement(&quot;BUTTON&quot;);
  btn.innerHTML = &quot;New Button&quot;;

  function appendButton(){
    document.getElementById(&quot;container&quot;).appendChild(btn);
  }
&lt;/script&gt;
&lt;div id=&#39;container&#39;&gt;
  &lt;button id=&#39;button1&#39; onclick=&quot;appendButton()&quot;&gt;Click Me&lt;/button&gt;
&lt;/div&gt;

答案2

得分: -1

以下是翻译好的部分:

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

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

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

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:

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

答案3

得分: -1

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

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

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

xml_add('before', id_('element_after'), '&lt;input type=&quot;button&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;');

xml_add('after', id_('element_before'), '&lt;input type=&quot;text&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;');

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

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

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;');

动态可重用代码:

function id_(id) {return (document.getElementById(id)) ? document.getElementById(id) : false;}

function xml_add(pos, e, xml)
{
 e = (typeof e == 'string' && id_(e)) ? id_(e) : e;

 if (e.nodeName)
 {
  if (pos=='after') {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true),e.nextSibling);}
  else if (pos=='before') {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true),e);}
  else if (pos=='inside') {e.appendChild(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true));}
  else if (pos=='replace') {e.parentNode.replaceChild(document.importNode(new DOMParser().parseFromString(xml,'application/xml').childNodes[0],true),e);}
  //添加片段并将其返回。
 }
}
英文:

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.

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;);

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;);

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):

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:

function id_(id) {return (document.getElementById(id)) ? document.getElementById(id) : false;}

function xml_add(pos, e, xml)
{
 e = (typeof e == &#39;string&#39; &amp;&amp; id_(e)) ? id_(e) : e;

 if (e.nodeName)
 {
  if (pos==&#39;after&#39;) {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true),e.nextSibling);}
  else if (pos==&#39;before&#39;) {e.parentNode.insertBefore(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true),e);}
  else if (pos==&#39;inside&#39;) {e.appendChild(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true));}
  else if (pos==&#39;replace&#39;) {e.parentNode.replaceChild(document.importNode(new DOMParser().parseFromString(xml,&#39;application/xml&#39;).childNodes[0],true),e);}
  //Add fragment and have it returned.
 }
}

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:

确定