【问题标题】:jQuery code is working on JSFiddle, but not in my web appjQuery 代码在 JSFiddle 上运行,但不在我的网络应用程序中
【发布时间】:2013-04-10 16:50:41
【问题描述】:

我在使用 jQuery 时遇到了一个奇怪的问题。我在页面上有一组按钮,当长按时,它们会在按钮的位置弹出一个输入框。 Here's the fiddle.

HTML

<div class="popupView" id="recoveryView">
    <button class="intervalBtn" type="button" name="recovery0" id="recovery0">00:30</button>
    <button class="intervalBtn" type="button" name="recovery6" id="recovery6">00:35</button>
    <button class="intervalBtn" type="button" name="recovery1" id="recovery1">02:00</button>
    <button class="intervalBtn" type="button" name="recovery7" id="recovery7">03:00</button>
    <button class="intervalBtn" type="button" name="recovery2" id="recovery2">04:00</button>
    <button class="intervalBtn" type="button" name="recovery8" id="recovery8">05:00</button>
</div>
<input id="edit_interval" type="text" />

CSS

.popupView {
    background: #999999;
    -webkit-border-radius: 6px;
    -webkit-box-shadow: 0 0 4px #333333;
    width: 30%;
    height: 70%;
    position: absolute;
}
.intervalBtn {
    color: blue;
    background: #CCCCCC;
    -webkit-border-radius: 6px;
    width: 41%;
    height: 12.5%;
    float: left;
    margin-left: 6%;
    margin-top: 3%;
    font: 12pt sans-serif;
}
.recoveryBtn {
    color: blue;
    background: #CCCCCC;
    -webkit-border-radius: 6px;
    width: 88%;
    height: 12.5%;
    float: left;
    margin-left: 6%;
    margin-top: 3%;
    font: 12pt sans-serif;
}
.intervalBtn.selected, .recoveryBtn.selected {
    color: #CCCCCC;
    background: blue;
}
body {
    position:absolute;
    overflow: hidden;
    bottom: 0;
    left: 0;
    right: 0;
    top: 0;
    user-select: none;
    -moz-user-select: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
body > div {
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
#edit_interval {
    display: none;
    position: absolute;
}

JavaScript

$(".intervalBtn").click(function () {
    $(".intervalBtn").removeClass("selected");
    $(this).addClass('selected');
}).mouseup(function () {
    clearTimeout(pressTimer);
    return false;
}).mousedown(function () {
    // Set timeout
    pressTimer = window.setTimeout(function () {
        console.log("interval button long click");

        $("#edit_interval").css("display", "block").css("left", $(this).position().left).css("top", $(this).position().top);
    }, 1000)
    return false;
});

如果在小提琴中运行,唯一的问题是盒子的位置(我的左侧和顶部位置是否不正确?),但是当我在实际的网络应用程序(在 Safari 中)上测试时,我收到以下错误长按日志后出现无输入框:

TypeError: 'undefined' 不是 'in' 的有效参数(评估 't in e')

jQuery.min.js: 5

所以这是一个由两部分组成的问题(重点放在第 1 部分):

  1. 为什么会出现此错误(是否是错误?),我该如何解决?
  2. 如何将输入框设置到正确的位置?

编辑

按照 cmets 中的建议,我将 jquery.min.js 换成了 jquery.js,并且出现了更容易调试的错误:

TypeError: 'undefined' 不是 'in' 的有效参数(评估 'name in style')

在第 6643 行。这是来自jQuery.js 的代码:

// return a css property mapped to a potentially vendor prefixed property
function vendorPropName( style, name ) {

    // shortcut for names that are not vendor prefixed
    if ( name in style ) {   //<---This is the line that causes the error
        return name;
    }

    // check for vendor prefixed names
    var capName = name.charAt(0).toUpperCase() + name.slice(1),
        origName = name,
        i = cssPrefixes.length;

    while ( i-- ) {
        name = cssPrefixes[ i ] + capName;
        if ( name in style ) {
            return name;
        }
    }

    return origName;
}

这是否意味着jQuery找不到CSS属性lefttop

【问题讨论】:

  • (试一试):你能试试吗:(function ($) { //your jQuery code here }(jQuery)); 可能是 jQuery noconflict 模式的问题
  • @karthikr,我在哪里添加?我尝试将它放在我的 jQuery 代码之前,但它不起作用。
  • 用这个代替$(document.ready()
  • 这不起作用。感谢您的尝试!
  • 我可以推荐的另一件事是,您可以将 min 替换为普通的 js,看看您是否能理解发生了什么。很抱歉没有提供帮助

标签: javascript jquery html typeerror


【解决方案1】:

对于您问题的第 2 部分,您的 this 不是您期望的 this,因为它的范围在 window.setTimeout() 函数内。您需要在setTimeout() 的范围之外获得对this 的引用(或只是单击元素的位置)。相关代码如下。我更新了你的小提琴here。请注意,position 函数不考虑边距或填充。

var pos = $(this).position();

// Set timeout
pressTimer = window.setTimeout(function() 
{ 
    console.log("interval button long click");

    $("#edit_interval").css({"left": pos.left + 'px', "top": pos.top + 'px'}).show();
},1000)
return false;

【讨论】:

  • 这解决了这两个问题!它一定是对this 的错误引用。这让我真的卡住了 - 非常感谢(+1 并接受)!
猜你喜欢
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多