最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bookmarklet: inserting text into textarea with js? - Stack Overflow

programmeradmin2浏览0评论

What I am doing wrong?

 javascript:document.getElementsByTagName('textarea').innerHTML='inserted';

I want to create a bookmarklet to insert simple text to a textarea on a given webpage.

What I am doing wrong?

 javascript:document.getElementsByTagName('textarea').innerHTML='inserted';

I want to create a bookmarklet to insert simple text to a textarea on a given webpage.

Share Improve this question edited Nov 26, 2020 at 19:16 peterh 1 asked Oct 1, 2010 at 6:59 mattmatt 44.4k107 gold badges268 silver badges402 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Use the value property rather than innerHTML and make sure your code evaluates to undefined, which you can do by wrapping it in a function with no return statement. If you don't do this, the contents of the page will be replaced with whatever your code evaluates to (in this case, the string 'inserted').

javascript:(function() {document.getElementsByTagName('textarea')[0].value = 'inserted';})();

Update 14 January 2012

I failed to spot the fact that the original code was treating document.getElementsByTagName('textarea') as a single element rather than the NodeList it is, so I've updated my code with [0]. @streetpc's answer explains this in more detail.

Unlinke getElementById, getElementsByTagName has an sat Elements because it returns an array array-like NodeList of the matching elements. So you'll have to access one of the elements first, let's say the first one for simplicity:

javascript:void((function(){document.getElementsByTagName('textarea')[0].value='inserted'})())

Also, as mentioned by others, value property rather than innerHTML here.

In case anyone wonders how to use the currently focused text field, use the following:

document.activeElement.value = "...";

In jQuery you have to use if this way:

for single element -> $('#element_id').html('your html here')
for all text areas -> $('textarea').val('your html here')

I have to confess that I`m not sure why it works this way but it works. And use rameworks, they will save you time and nerves.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>