提取要在 jquery 函数中使用的链接 ID

Extracting a link id to use within a jquery function

本文关键字:链接 ID jquery 函数 提取      更新时间:2023-09-26

all.

我知道这相对简单,但我对javascript的经验不足。背景是我正在尝试在同一页面上使用具有单独模态的简单模态。这种事情之前已经解释过(http://code.google.com/p/simplemodal/issues/detail?id=32),但我不太了解 this.id,并且代码不起作用。

我认为我的术语在这里甚至不正确,但是选择器没有正确检索我的链接想法并使用"#osx_"字符串解析它。使用警报后,我意识到了这一点(实际上是几个小时后)。

$('a.osx').click(function () {
$('#osx_' + this.id).modal();
});

我知道我的 HTML 设置正确,如果我将"#osx_newsletter"硬编码到两个位置,它就可以正常工作。这显然会杀死我需要的动态 id。

这是代码。有人可以让我知道我在这里做错了什么吗?我将不胜感激!谢谢!

/*
 * SimpleModal OSX Style Modal Dialog
 * http://simplemodal.com
 *
 * Copyright (c) 2013 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 */
jQuery(function ($) {
    var OSX = {
        container: null,
        init: function () {
            $("a.osx").click(function (e) {
                e.preventDefault(); 
alert('#osx_'+$(this).attr('id'));
                $('#osx_' + 'donate').modal({
                    overlayId: 'osx-overlay',
                    containerId: 'osx-container',
                    closeHTML: null,
                    minHeight: 80,
                    opacity: 65, 
                    position: ['0',],
                    overlayClose: true,
                    onOpen: OSX.open,
                    onClose: OSX.close
                });
            });
        },
        open: function (d) {
            var self = this;
            self.container = d.container[0];
            d.overlay.fadeIn('slow', function () {
                $('#osx_' + 'donate', self.container).show();
                var title = $("#osx-modal-title", self.container);
                title.show();
                d.container.slideDown('slow', function () {
                    setTimeout(function () {
                        var h = $("#osx-modal-data", self.container).height()
                            + title.height()
                            + 20; // padding
                        d.container.animate(
                            {height: h}, 
                            200,
                            function () {
                                $("div.close", self.container).show();
                                $("#osx-modal-data", self.container).show();
                            }
                        );
                    }, 300);
                });
            })
        },
        close: function (d) {
            var self = this; // this = SimpleModal object
            d.container.animate(
                {top:"-" + (d.container.height() + 20)},
                500,
                function () {
                    self.close(); // or $.modal.close();
                }
            );
        }
    };
    OSX.init();
});

你可以试试这种方式:

$('a.osx').click(function () {
    $('[id="osx_' + this.id+'"]').modal();
});