我以前从未使用过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>



  注意:在进行所有操作之前,请确保将id demo1设置为您的文本区域


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

关于javascript - 如何调用jQuery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33973519/

10-12 00:24
查看更多