2015-11-28 3 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> 

Примечание: прежде чем все обязательно установить demo1 Id вашего текстового поле

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

Я думаю, что лучший способ, чтобы определить свою кнопку с помощью id и Asign события щелчка к нему с помощью 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 
     }); 
    }); 
}); 

Надеется, что это помогает.

Смежные вопросы