简体   繁体   中英

Could not find element with id maincanvas in jQuery?

This is the error I get

uncaught exception: Could not find element maincanvas

I have included the jQuery file and checked the spelling but error seems to be because of something else

<a href="#" id="colorbtn" class="btn">Add Blue</a>           
<canvas id="maincanvas"></canvas>
<script type="text/javascript">
var canvas = $('#maincanvas');
var ctx    = canvas[0].getContext("2d");
var $color      = $('#colorbtn');

Caman('image.jpg','#maincanvas', function(){
this.revert(false);
this.render();
});

$color.on('click', function(e){
e.preventDefault();
Caman('image.jpg','#maincanvas', function(){
this.colorize(60, 105, 218, 10);
this.render();
});
});
</script>

What is causing this error?

As @ShaunakD says, wait for document-ready.

Also, if you're using CamanJS v4 the order of the image & canvas arguments has switched:

Caman("#maincanvas", "image.jpg", function() {});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM