jQuery offset question!!!

Asked 2 years ago, Updated 2 years ago, 36 views

* Question

$("#para").offset ({left: 50, top: 50}); Give the position left: 50, top: 50 here.

str += "left" is " + paraPosition.left + "pixel";

str + = "top is " + paraPosition.top + "pixel. ";       

If you try to output the position value of #para to parPosition.left, paraPosition.top instead of the position value, undefined is output! I wonder if I can correct this error Crying.

jquery javascript

2022-09-22 15:40

1 Answers

jQuery is specialized in chaining. Handing args over to $.offset(args...) returns the previously selected jQuery object. To receive the top/left as desired, you must call $.offset() without args.

So if you want to end it in one line,

var paraPosition =  $("#para").offset({left : 50 , top : 50}).offset();

If you untie it,

var $para = $("#para");
$para.offset({left : 50 , top : 50});
var paraPosition = $para.offset();


2022-09-22 15:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.