无法在 html 文本字段中输入文本
我有一个脚本,使我能够在 div 中拖动一些内容。
但是因为您可以四处拖动,所以无论您将光标放在 div 中的哪个位置,我都无法在文本字段中输入一些文本。
可以允许吗?
我可以轻松单击 div 容器中的链接。但不能在 html 输入字段中输入文本。
这是 javascript:
$(document).ready(function () {
$('#roadmap').mousedown(function (event) {
$(this)
.data('down', true)
.data('x', event.clientX)
.data('scrollLeft', this.scrollLeft);
return false;
}).mouseup(function (event) {
$(this).data('down', false);
}).mousemove(function (event) {
if ($(this).data('down') == true) {
this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
}
}).mousewheel(function (event, delta) {
this.scrollLeft -= (delta * 30);
}).css({
'overflow': 'hidden',
'cursor': 'col-resize'
});
});
$(window).mouseout(function (event) {
if ($('#roadmap').data('down')) {
try {
if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
$('#roadmap').data('down', false);
}
} catch (e) { }
}
});
这是我的扩展鼠标滚轮功能(如果需要)
(function ($) {
$.event.special.mousewheel = {
setup: function () {
var handler = $.event.special.mousewheel.handler;
// Fix pageX, pageY, clientX and clientY for mozilla
if ($.browser.mozilla)
$(this).bind('mousemove.mousewheel', function (event) {
$.data(this, 'mwcursorposdata', {
pageX: event.pageX,
pageY: event.pageY,
clientX: event.clientX,
clientY: event.clientY
});
});
if (this.addEventListener)
this.addEventListener(($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
else
this.onmousewheel = handler;
},
teardown: function () {
var handler = $.event.special.mousewheel.handler;
$(this).unbind('mousemove.mousewheel');
if (this.removeEventListener)
this.removeEventListener(($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
else
this.onmousewheel = function () { };
$.removeData(this, 'mwcursorposdata');
},
handler: function (event) {
var args = Array.prototype.slice.call(arguments, 1);
event = $.event.fix(event || window.event);
// Get correct pageX, pageY, clientX and clientY for mozilla
$.extend(event, $.data(this, 'mwcursorposdata') || {});
var delta = 0, returnValue = true;
if (event.wheelDelta) delta = event.wheelDelta / 120;
if (event.detail) delta = -event.detail / 3;
if ($.browser.opera) delta = -event.wheelDelta;
event.data = event.data || {};
event.type = "mousewheel";
// Add delta to the front of the arguments
args.unshift(delta);
// Add event to the front of the arguments
args.unshift(event);
return $.event.handle.apply(this, args);
}
};
$.fn.extend({
mousewheel: function (fn) {
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
},
unmousewheel: function (fn) {
return this.unbind("mousewheel", fn);
}
});
})(jQuery);
这是 HTML
<div id="roadmap">
<ul>
<li class="roadmap_description">
<h2>Welcome</h2>
<p>Description</p>
</li>
<li><h3 class="milestone_name">v. 1.0.2.3</h3>
<ul>
<li class="milestone_item"><a href="#">Title description</a></li>
</ul>
<div class="milestone_item_add">
<input class="field" name="milestone_item_name" type="text" /><a href="#"><img src="/Intranet/admin/Intranet/css/images/icons/wand.png" alt="Add new" /></a>
</div>
</li>
</ul>
</div>
谢谢。
I have a script that makes me able to drag some content in a div.
But because you can drag around, no matter where you place the cursor in the div, I am not able to input some text in a text field.
Is it possible to allow that?
I can eaily click links in the div container. But not input text in a html input field.
This is the javascript:
$(document).ready(function () {
$('#roadmap').mousedown(function (event) {
$(this)
.data('down', true)
.data('x', event.clientX)
.data('scrollLeft', this.scrollLeft);
return false;
}).mouseup(function (event) {
$(this).data('down', false);
}).mousemove(function (event) {
if ($(this).data('down') == true) {
this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
}
}).mousewheel(function (event, delta) {
this.scrollLeft -= (delta * 30);
}).css({
'overflow': 'hidden',
'cursor': 'col-resize'
});
});
$(window).mouseout(function (event) {
if ($('#roadmap').data('down')) {
try {
if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
$('#roadmap').data('down', false);
}
} catch (e) { }
}
});
This is my extended mousewheel function (if needed)
(function ($) {
$.event.special.mousewheel = {
setup: function () {
var handler = $.event.special.mousewheel.handler;
// Fix pageX, pageY, clientX and clientY for mozilla
if ($.browser.mozilla)
$(this).bind('mousemove.mousewheel', function (event) {
$.data(this, 'mwcursorposdata', {
pageX: event.pageX,
pageY: event.pageY,
clientX: event.clientX,
clientY: event.clientY
});
});
if (this.addEventListener)
this.addEventListener(($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
else
this.onmousewheel = handler;
},
teardown: function () {
var handler = $.event.special.mousewheel.handler;
$(this).unbind('mousemove.mousewheel');
if (this.removeEventListener)
this.removeEventListener(($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
else
this.onmousewheel = function () { };
$.removeData(this, 'mwcursorposdata');
},
handler: function (event) {
var args = Array.prototype.slice.call(arguments, 1);
event = $.event.fix(event || window.event);
// Get correct pageX, pageY, clientX and clientY for mozilla
$.extend(event, $.data(this, 'mwcursorposdata') || {});
var delta = 0, returnValue = true;
if (event.wheelDelta) delta = event.wheelDelta / 120;
if (event.detail) delta = -event.detail / 3;
if ($.browser.opera) delta = -event.wheelDelta;
event.data = event.data || {};
event.type = "mousewheel";
// Add delta to the front of the arguments
args.unshift(delta);
// Add event to the front of the arguments
args.unshift(event);
return $.event.handle.apply(this, args);
}
};
$.fn.extend({
mousewheel: function (fn) {
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
},
unmousewheel: function (fn) {
return this.unbind("mousewheel", fn);
}
});
})(jQuery);
And here goes the HTML
<div id="roadmap">
<ul>
<li class="roadmap_description">
<h2>Welcome</h2>
<p>Description</p>
</li>
<li><h3 class="milestone_name">v. 1.0.2.3</h3>
<ul>
<li class="milestone_item"><a href="#">Title description</a></li>
</ul>
<div class="milestone_item_add">
<input class="field" name="milestone_item_name" type="text" /><a href="#"><img src="/Intranet/admin/Intranet/css/images/icons/wand.png" alt="Add new" /></a>
</div>
</li>
</ul>
</div>
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mousedown
中的return false
阻止按下鼠标按钮执行其默认操作,即聚焦子。
您可以在
mousedown
函数中测试event.target
来查看它是否是元素,如果是,则停止尝试处理它(立即
返回
或返回true
)。如果您希望当鼠标位于
时仍允许拖动元素,您仍然可以
return false
但手动调用focus()
在mousedown
中的输入上。这样做的缺点是用户无法像输入通常允许的那样使用拖动来选择部分文本。The
return false
inmousedown
prevents the mouse button press from having its default action, which is to focus the child<input>
.You could test
event.target
in themousedown
function to see if it's the<input>
element, and if so just stop trying to handle it (return
orreturn true
straight away).If you wanted to still allow the element to be dragged when the mouse is in the
<input>
, you could stillreturn false
but manually callfocus()
on the input inmousedown
. The disadvantage of that would be that the user couldn't use drags to select part of the text, as inputs would normally allow.为什么不将鼠标悬停事件添加到设置此的文本字段: $(this).data('down', false); ?
Why dont you add a mouseover event to the textfield that sets this: $(this).data('down', false); ?