如何调用 jquery

How to call a jquery

我以前从未使用过 Jquery,我是如何从按钮的 onclick 调用以下函数来使用 textarea

函数

$(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>

提前感谢您的帮助

<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>

Note: before everything be sure to set the Id demo1 to your textarea

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

我认为更好的方法是通过 id 识别您的按钮并使用 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
        });
    });
});

希望对您有所帮助。