开发者

problem in writing a function in jquery

this is my raw javascript code :

<script language="javascript" type="text/javascript" > 
  function S2T(e) {
  document.comments.comment.value = document.comments.comment.value + e;
  } 
</SCRIPT>

and i want to convert it to jquery function

<script language="javascript" type="开发者_开发知识库text/javascript" > 
$(document).ready(function() {
  function S2T(e) {
  $("textarea#comment").val($('#comment').val()+e); 
 });
</SCRIPT>

html part

<a href='javascript:S2T('please')' >

i cant convert it , i just want to write function when i click on a icon , it adds to textarea


Writing function definition in to $(document).ready() binds it to the ready event of the document. This means the code will execute once the page has finished loading. This isn't what you wanted.

Since you have jQuery loaded, you can simply use it within your already existing function, such as:

<script  language="javascript" type="text/javascript">
function S2T(e) {
  $("textarea#comment").val($('#comment').val()+e);
}
</script>

And use it with your existing html:

<a href='javascript:S2T("please")' >


You can define functions just as you used to, no need to put it in the ready() call:

<script language="javascript" type="text/javascript" > 
  function S2T(e) {
     $("textarea#comment").val($('#comment').val()+e); 
  }
</script>

then make sure you don't use single-quoted javascript strings inside single-quoted HTML attributes:

<a href='javascript:S2T("please")' >


<script language="javascript" type="text/javascript" > 
  function S2T(e) {
    $("textarea#comment").val($('#comment').val()+e); 
  }
</script>

The document ready function is not necessary here. Also, you had a quote missing in the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜