JQuery Copy text&粘贴到textarea [英] JQuery Copy text & paste into textarea
问题描述
我创建了一个javascript函数,它将占用隐藏的跨度,复制该范围内的文本并将其插入到网站上的单个textarea标记中。我已经用JavaScript编写了一个函数来执行此操作(好吧,有点,只需点击几下),但我知道有更好的方法 - 任何想法?该行为类似于Twitter的转推,但使用博客上的帖子部分代替。哦,我也在标题中调用jquery。
I've created a javascript function that will take a hidden span, copy the text within that span and insert it into a single textarea tag on a website. I've written a function in JavaScript that does this (well, kinda, only after a few clicks), but I know there's a better way - any thoughts? The behavior is similar to a Retweet for twitter, but using sections of a post on a blog instead. Oh, and I'm also calling out to jquery in the header.
<script type="text/javascript">
function repost_submit(postID) {
$("#repost-" + postID).click(function(){
$("#cat_post_box").empty();
var str = $("span#repost_msg-" + postID).text();
$("#cat_post_box").text(str);
});
}
</script>
推荐答案
$("#repost-" + postID).click(function(){
$("#cat_post_box").val(''); // Instead of empty() - because empty remove all children from a element.
$("#cat_post_box").text($("#repost_msg-" + postID).text());//span isn't required because you have and id. so the selector is as efficient as it can be.
});
并将所有内容包装在$(文档).ready(function(){/ Insert中这里的代码 /})以便在加载DOM时绑定到$(#repost-+ postID)按钮或链接。
And wrap everything in a $(document).ready(function(){ /Insert the code here/ }) so that it will bind to $("#repost-" + postID) button or link when the DOM is loaded.
这篇关于JQuery Copy text&粘贴到textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!