将变量传递给 JavaScript 函数

发布于 2024-12-29 18:33:47 字数 593 浏览 0 评论 0原文

您好,有人可以告诉我为什么此代码不正确吗?我正在尝试将两个变量值传递给函数。

$( "#resizable-text1, #resizable-text2" ).draggable({
    containment: "#containment-wrapper1",
    scroll: false, 
    stop: function(event, ui)
    {
        var id = $(this).attr('data-idSuffix');
        adjust_pos($('#resizable-text',id));
    }
});

function adjust_pos(elem, id) {
    alert('elem = '+elem)
    alert('id = '+id)
    var currentPos = $("#"+elem+id).position()
    var xpos = parseInt(currentPos.left)
    var ypos = parseInt(currentPos.top)

    then use variables ele and id...
}

任何帮助表示赞赏。

Hi can someone please tell me why this code is incorrect? I'm trying to pass two variable values to a function.

$( "#resizable-text1, #resizable-text2" ).draggable({
    containment: "#containment-wrapper1",
    scroll: false, 
    stop: function(event, ui)
    {
        var id = $(this).attr('data-idSuffix');
        adjust_pos($('#resizable-text',id));
    }
});

function adjust_pos(elem, id) {
    alert('elem = '+elem)
    alert('id = '+id)
    var currentPos = $("#"+elem+id).position()
    var xpos = parseInt(currentPos.left)
    var ypos = parseInt(currentPos.top)

    then use variables ele and id...
}

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜中书 2025-01-05 18:33:47

您不需要将字符串包装在 jQuery $() 函数中,因为在您的函数中您无论如何都会这样做 ($("#"+elem+id).position( ))。只需发送字符串即可。

所以这应该有效:

adjust_pos('#resizable-text', id);

编辑: 另外,无论是在您的函数 adjust_pos 中还是在您传递的参数中,您都应该删除 # 因为否则会出现是两个。我建议您在函数中删除,因此如果由于某种原因您想发送类选择器,您也可以这样做。

var currentPos = $(elem+id).position();

You don't need to wrap the string in the jQuery $() function, because in your function you will do that anyways ($("#"+elem+id).position()). Simply send the string.

So this should work:

adjust_pos('#resizable-text', id);

EDIT: Also, either in your function adjust_pos or in the parameter you pass you should remove a # because otherwise there would be two. I recommend you remove in the function, so if for some reason you want to send a class selector, you can do that too.

var currentPos = $(elem+id).position();
生生漫 2025-01-05 18:33:47

根据您的 adjustment_pos 函数代码,它就像是 adjustment_pos('resizing-text', id);

According your code of adjust_pos function, it is like to be adjust_pos('resizable-text', id);

匿名的好友 2025-01-05 18:33:47

看起来你的第二个右括号位于错误的位置:

adjust_pos($('#resizable-text'),id);

Update

正如注释中所指出的,考虑到函数对参数所做的操作,您实际上不需要传入 jQuery 对象。您只需将元素的 id 作为字符串传递即可。

给定您当前的代码,您可以执行以下操作:

adjust_pos('resizable-text',id);

或者对 japrescott 建议的代码进行调整(从函数中删除哈希并将其作为参数的一部分传递):

adjust_pos('#resizable-text',id);

Looks like your second closing bracket is in the wrong place:

adjust_pos($('#resizable-text'),id);

Update

As has been pointed out in the comments, you don't actually need to pass in the jQuery object given what your function does with the parameter. You simply need to pass in the id of the element as a string.

Given your current code you can do:

adjust_pos('resizable-text',id);

or with the adjustment to your code that japrescott recomended (removing the hash from the function and passing it in as part of the parameter):

adjust_pos('#resizable-text',id);
彩虹直至黑白 2025-01-05 18:33:47

这条线;

 adjust_pos($('#resizable-text',id));

一定是这样的;

 adjust_pos($('#resizable-text'),id);

this line;

 adjust_pos($('#resizable-text',id));

must be like this;

 adjust_pos($('#resizable-text'),id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文