This is my code. It works to Firefox, Chrome but not in ie7.
arr makes (number + 1) for every line..
$("#div-id1 a").attr("onclick", function (arr) {
return "document.getElementById('row1').value+=" + arr;
});
$("#div-id2 a").attr("onclick", function (arr) {
return "document.getElementById('row2').value+=" + arr;
});
<input id="row1" type="text" name="row1" value="" />
<input id="row2" type="text" name="row2" value="" />
My js codes are between this.
jQuery(function($){...code...});
I am adding onclick, because links are looking like this.
<a>Hello World</a>
Code output is :
<a onclick="document开发者_如何学Python.getElementById('row1').value+=1">Hello World</a>
Can someone show me working example ?
You're doing it wrong.
Will adding a function to the onclick
attribute work? yes, in some browsers, but you're not making good use of jQuery's normalized events. The jQuery way of setting a click event is to use click(fn)
, or bind('click', fn)
or live('click', fn)
Additionally, since you haven't shown where your script is in relation to the HTML, it's possible that the dom elements dont exist yet. Using live
will delegate the event to the document so that it continues to detect new elements that match the selector. Alternatively you could use the document ready
event:
Long form:
jQuery(document).ready(function($){...code...});
Short form:
jQuery(function($){...code...});
For more info you should read the jQuery api.
Put it into a $(document).ready(...
:
$(document).ready(function() {
$("#div-id1 a").attr("onclick", function (arr) {
return "document.getElementById('row1').value+=" + arr;
});
$("#div-id2 a").attr("onclick", function (arr) {
return "document.getElementById('row2').value+=" + arr;
});
});
Please read http://api.jquery.com/ready/
Don't set attribute onclick - pass a function to the jquery click function
$("#div-id1 a").click( function () {
/* Code here */
});
Can you explain further about arr though? You would need to make some modifications to the code to make it work with click instead of attr.
精彩评论