【问题标题】:jquery not performing the way correctjquery 没有以正确的方式执行
【发布时间】:2014-09-25 11:48:11
【问题描述】:

我有一些 jquery 先给你看

$(function(){
    function darkBox(div){
        var w = (div.attr('width')) ? div.attr('width') : div.width();

        var h = (div.attr('height')) ? div.attr('height') : div.height();

        var box = $('<div></div>').addClass('darkCover');
        $('body').prepend(box);
        box.fadeTo('fast', 0.8);

        var contentBox = $('<div></div>').html(div.html());
        contentBox.addClass('darkContent');

        var x = $(window).width()/2;
        var y = $(window).height()/2;
        var startW = h-y/2;
        var startH = w-x/2;
        var endTop = y - h/2;
        var endLeft = x - w/2;

        contentBox.css("left", x+"px");
        contentBox.css("top", startW+"px");
        contentBox.css("z-index", "910");
        contentBox.css("width", w+"px");
        contentBox.css("height", h+"px");

        $('body').prepend(contentBox);
        // contentBox.fadeIn('slow')
        contentBox.animate({
            opacity: 1,
            width:w+"px",
            height:h+"px",
            top:endTop+"px",
            left:endLeft+"px"
        }, 1000, "easeOutExpo");

        box.click(function(){
            box.fadeOut();
            contentBox.fadeOut();
        });
    }

    $('.darkBox').each(function(){
        var div = $($(this).attr('href'));
        div.hide();
        $(this).click(function(){
            darkBox(div);
        });
    });
});

我的css也在下面

.darkContent{
    position: fixed;
    background-color: white;
    border: 5px solid black;
    padding: 8px;
    overflow: hidden;
    color: #333;
    font-family: arial;
}
.darkCover{
    position: fixed;
    left: 0px;
    top: 0px;
    z-index: 900;
    background-color: black;
    opacity: 0;
    width: 100%;
    height: 100%;
}

和html

<a href="#useThisDiv" class="btn blue btn-xs darkBox">Show Box</a>
<div id="useThisDiv" width="500" height="500">
    <table>
        <tr>
            <td>
                array index and a corresponding array value each time. (The value can also be accessed through the this keyword, 
            </td>
        </tr>
        <tr>
            <td>
                array index and a corresponding array value each time. (The value can also be accessed through the this keyword, 
            </td>
        </tr>
        <tr>
            <td>
                array index and a corresponding array value each time. (The value can also be accessed through the this keyword, 
            </td>
        </tr>
    </table>
</div>

我不知道问题出在哪里,但我的问题是,当我单击带有 darkBox 类的锚标记时,它只是将我重定向到另一个页面。它实际上创建了 var box div 并在一秒钟内消失了我当前的 jquery 没有这个就可以正常工作..请帮助

如果您无法解决这个问题,我只想在用户每次单击按钮时打开一个弹出 div,背景应该变得稍微不透明,用户可以在该框上输入数据。

有什么想法吗?

【问题讨论】:

  • 如果您在锚点点击时阻止Default() 怎么办? $(this).click(function(e){ e.preventDefault(); darkBox(div); });
  • @SSA 它仍然会进入索引页面...不知道为什么:(

标签: javascript jquery html css show-hide


【解决方案1】:

您需要使用preventDefault 防止单击事件的默认行为。现在您的浏览器将被重定向到锚点#useThisDiv。

将您的代码更新为:

$('.darkBox').each(function(){
    var div = $($(this).attr('href'));
    div.hide();
    $(this).click(function(event){
        event.preventDefault();
        darkBox(div);
    });
});

或者,使用data- 属性而不是href 来定义这种额外的功能。见this Fiddle

【讨论】:

  • 但是阻止这行代码box.click(function(){ box.fadeOut();contentBox.fadeOut();}); 不能让它消失,这不应该是解决方案,因为我没有要求它转到任何页面,只需打开驻留在该页面上的 div
  • 感谢您的努力,尽管我为您的努力按下了向上箭头,但如果您知道任何其他方式来实现这一点,我很乐意遵循这些说明。
  • 稍后我会更新 Fiddle,用于移除框的点击处理程序会立即触发,而不是在第二次点击时触发。
  • 你能查一下吗?我已经更新了小提琴。您需要注意内存泄漏,因为此代码非常容易泄漏。一些重组会有所帮助。 (但 IMO 这超出了您的问题范围)
  • 我将 #useThisDiv 从 href 移动到 data-target 并在创建 box 变量后立即移动了点击处理程序以删除覆盖。可能与 JavaScript 如何提升函数和变量声明有关,但正如我所说,您需要检查此代码是否存在内存泄漏:)。很高兴帮助和 GL 学习 jQuery。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
相关资源
最近更新 更多