繁体   English   中英

使用 jQuery 在页面加载时将文本复制到输入字段

[英]Copy text to input field on page load using jQuery

我想在页面加载时将文本This is Sparta复制到输入字段中。 我想知道如何使用 jQuery 或 Javascript 来解决这个问题。

<p id="addTag">This is Sparta</p>
<input name="showTag" type="text" id="showTag" />

任何人?

使用香草 JavaScript:

 document.addEventListener('DOMContentLoaded', (event) => { document.getElementById('showTag').value = document.getElementById('addTag').textContent; });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="addTag">This is Sparta</p> <input name="showTag" type="text" id="showTag" />

使用 jQuery:

 $(document).ready(function(){ $('#showTag').val($('#addTag').text()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="addTag">This is Sparta</p> <input name="showTag" type="text" id="showTag" />

JQuery:

$( document ).ready(function() {
    $('#showTag').val($('#addTag').html());
});

暂无
暂无

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

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