Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

javascript - onclick event handler doesn't fire

I have the following code:

<div class="modal-footer">
   <a class="btn btn-primary" onclick="saveChanges ();">Save changes</a>
</div>

<script type="text/javascript">
$(document).ready(function() {
   function saveChanges () {
      tinyMCE.triggerSave();
   } 
   ...
   ...

I'm not sure why but when I click on the "Save changes" link then nothing happens and it does not seem to call the saveChanges function.

Is there a way that I could remove the onClick event from the link and just have it so that the saveChanges() functionality is attached to the clicking of the link with jQuery?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function should be in the global scope, if it's in an inner scope it's not accessible to the onclick=...:

<script type="text/javascript">
   function saveChanges () {
      tinyMCE.triggerSave();
   } 
</script>

or with jQuery:

<a class="btn btn-primary"> Save changes</a>

<script type="text/javascript">
$(function(){
    $('.btn-primary').click(function(){
        tinyMCE.triggerSave();
    });
});
</script>

It would be a good idea to give the <a> an id like this:

<a id="theAnchor"> Save changes</a>

Then...

$(function(){
    $('#theAnchor').click(function(){
        tinyMCE.triggerSave();
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...