JQuery setting onclick attribute working only on FireFox?

I am trying to set the onclick attribute for a link generated using jQuery.

While other attributes work without problems (for example, href, but also other user attributes), setting " onclick " only works in FireFox . In other browsers, the attribute is simply ignored.

Any hint? Is this behavior right? And why?

<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
  jQuery(function(){
    var obj = $('<a/>');
    obj.html('click me');
    obj.attr({'href':'#test', 'onclick':'alert("o hai"); return false;'});
    $('body').append(obj);
  });
</script>
</head><body /></html>

EDIT : I know about the jQuery.click () method; but it’s not, I just want to set the attribute and cannot understand why it doesn’t work, like any other attr.

0
source share
2

?

attr('onsomeevent', ...) , Firefox - , .

?

IE 8 / HTML. setAttribute() DOM . onclick , . .

jQuery attr() , attr, DOM, HTML... HTML (, class), DOM (, className).

( , , . , IE . jQuery.)

, JavaScript - . jQuery JavaScript (, inline function() { ... }).

+4

jQuery ....

<script type="text/javascript">
  jQuery(function(){
    var obj = $('<a/>');
    obj.html('click me')
       .attr('href','#test')
       .click(function() {
            alert("o hai");
            return false;
        })
    $('body').append(obj);
  });
</script>

: onclick onClick, "C"

jQuery(function(){
    var obj = $('<a/>');
    obj.html('click me');
    obj.attr({'href':'#test', 'onClick':'alert("o hai"); return false;'});
    $('body').append(obj);
  });
+2

Source: https://habr.com/ru/post/1745394/


All Articles