简体   繁体   English

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

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

I would like to copy text This is Sparta into the input field on page load.我想在页面加载时将文本This is Sparta复制到输入字段中。 I'm wondering how to go about this using jQuery or Javascript.我想知道如何使用 jQuery 或 Javascript 来解决这个问题。

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

Anyone?任何人?

Using vanilla JavaScript:使用香草 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" />

Using jQuery:使用 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: JQuery:

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

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

相关问题 如何使用 jQuery 在页面加载时关注表单输入文本字段? - How to focus on a form input text field on page load using jQuery? 使用javascript加载页面时自动将跨度文本复制到输入字段 - Copy span text to input field automatically when page load using javascript 如何使用jquery在页面加载时检查输入复选框字段的值 - How to check the input checkbox field with a value on page load using jquery 当输入字段隐藏时,jQuery将文本复制到剪贴板 - Jquery copy text to Clipboard when input field is hidden 使用jQuery在输入字段上获取更改的文本 - Get changed text on a input field using jQuery Jquery:如何使用 jQuery 在输入文本字段中只允许数字/浮点数(即使复制粘贴)? - Jquery: How to allow only numbers/float with comma (even when copy-pasted) in input text field using jQuery? 在文本字段中输入文本,单击按钮并在页面上获取结果(JQuery) - Input a text into a text field, click a button and get the result on the page (JQuery) 如何使 tabIndex(input field) 在页面加载时从顶部开始 - Jquery - How to make tabIndex(input field) to begin at the top on page load - Jquery 如何在页面加载时自动选择输入字段及其中的文本 - how to auto select an input field and the text in it on page load Angularjs:如何在页面加载时预先选择输入文本字段? - Angularjs: How to pre-select input text field on page load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM