内部HTML问题

发布于 2024-09-29 20:33:10 字数 687 浏览 0 评论 0原文

如何获取文本框的值以及 innerHTML

例如,如果我有,

<html>
   <head>
   </head>
        <body>
            <div id="getvalue">
          <p><input type="text" name="username" id="username"></p>
     </div>

       </body>
</html>

我需要特定 id="getvalue"innerHTML 以及文本框值。如果我使用我得到了结果

var gotvalue=document.getElementBYId('getvalue').innerHTML;

我刚刚得到了结果

"<p><input type="text" name="username" id="username"></p>"

另外,我需要文本框值以及innerHTML。我怎么能这样做呢?

提前致谢。

How do I get the values of textboxe's along with innerHTML?

For example, if I have

<html>
   <head>
   </head>
        <body>
            <div id="getvalue">
          <p><input type="text" name="username" id="username"></p>
     </div>

       </body>
</html>

I need the innerHTML of the particlular id="getvalue" along with the textbox value. I got the result if i use

var gotvalue=document.getElementBYId('getvalue').innerHTML;

I just got the result

"<p><input type="text" name="username" id="username"></p>"

Also, I need the textbox value along with the innerHTML. How could i do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-10-06 20:33:10

您需要 .value 属性 > 元素,如下所示:

var gotvalue = document.getElementById('getvalue').innerHTML;
var inpitvalue = document.getElementById('username').value;

You want the .value property of the <input> element, like this:

var gotvalue = document.getElementById('getvalue').innerHTML;
var inpitvalue = document.getElementById('username').value;
脱离于你 2024-10-06 20:33:10

由于我只能想象出一个半途而废的理由来说明您需要innerHTML(将其放在文档中的其他位置进行复制),因此我建议使用DOM方法来获取副本:cloneNode(true) 并使用 DOM 方法(哪一个取决于放置位置)将其注入到 DOM 中。
cloneNode() 也会保留属性,因此值也会被复制。
如果您确实需要该字符串,请忘记这个^^

As i can only imagine one halfway good reason for what you need the innerHTML(put it somewhere else into the document for copying), I would suggest to use the DOM-method to get the copy: cloneNode(true) and also use a DOM-Method(which one depends on where to put) to inject it into the DOM.
cloneNode() will preserve the attributes too, so the value will also be copied.
If you really need the string, forget this ^^

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文