2015-11-28 23 views
0

我從來沒有使用jQuery的之前,我是我如何從onclick調用按鈕的下面的函數與textarea如何調用一個jquery

功能

$(document).ready(function() { 
    $('#demo1').highlightTextarea({ 
     words: { 
      color: 'red', 
      words: ['N/A','n/a'] 
     }, 
     debug: true 
    }); 

一個例子上班我看的代碼如下:

HTML代碼

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> 
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script> 

    <link href="css/jquery.highlighttextarea.css" rel="stylesheet"> 
    <script src="js/jquery.highlighttextarea.js"></script> 
</head> 
<body> 

<textarea rows="4" cols="50"> 
This is a example n/a of all the following N/A 
Thanks 
</textarea> 

<button type="button" onclick= >Call function </button> 

<script type='text/javascript'> 
$(document).ready(function() { 
    $('#demo1').highlightTextarea({ 
     words: { 
      color: 'red', 
      words: ['N/A','n/a'] 
     }, 
     debug: true 
    }); 

}); 
</script> 

</body> 
</html> 

感謝您的幫助,提前

+0

你爲什麼要改變什麼? 我問過我想要的問題,而穆罕默德 - 尤瑟夫向我提供了我正在尋找的解決方案! –

回答

1
<script type='text/javascript'> 
$(document).ready(function() { 
    highlight_Textarea(); // to run the function after document ready 
}); 
function highlight_Textarea(){ 
    $('#demo1').highlightTextarea({ 
     words: { 
      color: 'red', 
      words: ['N/A','n/a'] 
     }, 
     debug: true 
    }); 
} 

</script> 

,並在HTML

<button type="button" onclick="highlight_Textarea()"></button> 

,或者你可以使用它沒有onclick屬性

<script type='text/javascript'> 
    $(document).ready(function() { 
     $('button[type="button"]').on('click',function(){ 
      $('#demo1').highlightTextarea({ 
       words: { 
       color: 'red', 
       words: ['N/A','n/a'] 
      }, 
       debug: true 
      }); 
     }); 
    }); 
</script> 

,並在HTML

<button type="button"></button> 

注:家居前務必到ID demo1的設置爲您的textarea

<textara id="demo1" ...... 
1

我認爲更好的辦法是通過id識別您的按鈕,並ASIGN click事件它使用on()到方法。

HTML:

<button type="button" id="my-button">Call funciton</button> 

JS:

$(document).ready(function() { 
    $('body').on('click' , '#my-button', function(){ 
     $('#demo1').highlightTextarea({ 
      words: { 
      color: 'red', 
      words: ['N/A','n/a'] 
      }, 
      debug: true 
     }); 
    }); 
}); 

希望這有助於。

相關問題