[英]How to convert jQuery code to JavaScript?
jQuery: jQuery:
var isResizing = false,
lastDownX = 0;
$(function () {
var container = $('#container'),
top = $('#top-panel'),
handle = $('#drag');
handle.on('mousedown', function (e) {
isResizing = true;
lastDownX = e.clientY;
});
$(document).on('mousemove', function (e) {
// we don't want to do anything if we aren't resizing.
if (!isResizing)
return;
var offsetRight = top.height() + (e.clientY - container.offset().top);
top.css('top', offsetRight);
}).on('mouseup', function (e) {
// stop resizing
isResizing = false;
});
});
JavaScript: JavaScript:
var isResizing = false
// eslint-disable-next-line no-unused-vars
var lastDownX = 0
function resizeVerticallyInit () {
var top = document.querySelector('#topology-container')
var handle = document.querySelector('#drag')
handle.addEventListener('mousedown', function (e) {
isResizing = true
lastDownX = e.clientY
})
document.addEventListener('mousemove', function (e) {
// we don't want to do anything if we aren't resizing.
if (!isResizing) {
return
}
var offsetRight = top.height() + 20
document.querySelector('#topology-container').style.top = offsetRight
})
document.addEventListener('mouseup', function (e) {
// stop resizing
isResizing = false
})
}
mounted () {
...
resizeVerticallyInit()
}
As you can see I use JavaScript function in Vue.js component in mounted() life-cycle method.如您所见,我在 mounted() 生命周期方法中的 Vue.js 组件中使用了 JavaScript function 组件。
I've tried to convert it, but it doesn't work.我试图转换它,但它不起作用。
First of all I needed to put:首先我需要说:
// eslint-disable-next-line no-unused-vars
var lastDownX = 0
I don't know why it's complaining that lastDownX is unused.我不知道为什么抱怨 lastDownX 未使用。
The second one is error:第二个是错误:
"[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'addEventListener' of null"
It's complaining about:它抱怨:
handle.addEventListener('mousedown', function (e) {
isResizing = true
lastDownX = e.clientY
})
Could someone help to convert it?有人可以帮忙转换吗?
First of document.querySelector
method will return null
if the element is not present in DOM.如果 DOM 中不存在该元素,则document.querySelector
方法的第一个方法将返回null
。 You cannot call addEventListener
on null.您不能在 null 上调用addEventListener
。 In jQuery
it worked because jquery constructs a separate object (called jQuery object).在jQuery
它起作用了,因为 jquery 构造了一个单独的 object(称为 ZF590B4FDA2C30BE28DD3C8C3CZB5C 对象)。 A jQuery object looks somewhat like this: jQuery object 看起来有点像这样:
{
0: {...} // HTMLElement reference
length: 1
} // jQuery object
This object exposes a jQuery API which has on
method for event handling (available regardless of presence of actual DOM element).这个 object 公开了一个 jQuery API,它具有事件on
方法(无论是否存在实际 DOM 元素都可用)。 This is a beauty of jQuery (and a drawback).这是 jQuery 的优点(也是一个缺点)。
Query selector method on the other hand returns the HTMLElement
.另一方面,查询选择器方法返回HTMLElement
。 If the element is not present in DOM then null
is returned.如果 DOM 中不存在该元素,则返回null
。
Now assuming that the element will appear in DOM after certain amount of time, there are two things you can do:现在假设元素会在一定时间后出现在 DOM 中,你可以做两件事:
live
events (event delegation) in JavaScript.在 JavaScript 中使用live
事件(事件委托)。I will show you the second approach:我将向您展示第二种方法:
setTimeout(() => { document.querySelector('#container').innerHTML = `<button id="btn">Click</button>`; }, 3000); document.addEventListener('click', (e) => { if (e.target.id === 'btn') { alert('Click works;'); } });
<div id="container"> A button will appear after 3 seconds. However, click event would still work. </div>
As you can see I am using e.target
to detect which element I clicked inside document (where I have attached the listener).如您所见,我正在使用e.target
来检测我在文档中单击了哪个元素(我在其中附加了侦听器)。 Once the condition matches, it triggers the alert
.一旦条件匹配,它就会触发alert
。 This allows us to bind events for elements that are not initially present in DOM.这允许我们为最初不存在于 DOM 中的元素绑定事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.