Javascript jQuery代码正在JSFIDLE上运行,但在我的web应用程序中没有
我在使用jQuery时遇到了一个奇怪的问题。我在页面上有一组按钮,当长时间单击时,意味着在按钮位置弹出一个输入框 HTMLJavascript jQuery代码正在JSFIDLE上运行,但在我的web应用程序中没有,javascript,jquery,html,typeerror,Javascript,Jquery,Html,Typeerror,我在使用jQuery时遇到了一个奇怪的问题。我在页面上有一组按钮,当长时间单击时,意味着在按钮位置弹出一个输入框 HTML <div class="popupView" id="recoveryView"> <button class="intervalBtn" type="button" name="recovery0" id="recovery0">00:30</button> <button class="intervalBtn"
<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" />
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;
});
如果在小提琴中运行,唯一的问题是框的位置(我是否得到了错误的左侧和顶部位置?)
TypeError:“undefined”不是“in”(计算“t in e”)的有效参数
在
jQuery.min.js:5
这是一个分为两部分的问题(重点是第1部分):
$(".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;
});
正如评论中所建议的,我将jquery.min.js
替换为jquery.js
,并且有一个更容易调试的错误:
TypeError:“undefined”不是“in”的有效参数(正在计算“样式中的名称”)
第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;
}
//返回映射到潜在供应商前缀属性的css属性
函数vendorPropName(样式、名称){
//未加供应商前缀的名称的快捷方式
如果(以样式命名){/对于问题的第2部分,您的this
不是您期望的this
,因为它的作用域位于窗口内。setTimeout()
函数。您需要在setTimeout()的作用域之外获取对this
的引用(或仅获取单击元素的位置)
。相关代码如下。我更新了您的小提琴。请注意,该函数不考虑边距或填充
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;
(只是一次尝试):你能试试吗:(函数($){//你的jQuery代码在这里}(jQuery));
这可能是jQuerynoconflict
mode@karthikr,我应该在哪里添加它?我尝试将它放在我的jQuery代码之前,但它不起作用。请在$(document.ready()的位置执行此操作
这不起作用。谢谢你的尝试!我可以推荐的另一件事是,你可以用普通的js替换min,看看你是否能理解发生了什么。很抱歉没有帮上忙。这解决了这两个问题!这一定是对的错误引用。
。这让我真的卡住了-非常感谢(+1&accept)!