jQuery和JS等效函数会产生不同的结果
jQuery and JS equivalent functions produce different results
我有以下cropper应用程序:
var canvas = $('#selector')[0]
//works
**canvas.width=image.width
canvas.height=image.height**
//doesn't work
**//$(canvas).width($(image).width())
//$(canvas).height($(image).height())**
//both seem to do the exact same thing
$('#selector').css('left','30px')
var ctx = $('#selector')[0].getContext("2d");
ctx.fillStyle="rgba(210,220,255,0.6)";
var cropinit=false;
//the cropped section will not be resizeable after the user finishes, but the user can create a new cropped section
canvas.addEventListener("mousedown", function setP1(event) {
//allows you to find the height and width by imagining a rectangle around it
//get bounding selector parameters
var selector_position = $('#selector').position()
xOff=selector_position.left
yOff=selector_position.top
console.log(xOff)
console.log(yOff)
p1=[event.clientX-xOff, event.clientY-yOff];
ctx.fillRect(80,54,40,40)
console.log(p1)
cropinit=true;
});
//so that if the user releases the mouse after it leaves the canvas, the crop completes
canvas.addEventListener("mouseleave", function() {
cropinit=false;
});
canvas.addEventListener("mousemove", function drawBox(event) {
if (cropinit) {
p2=[event.clientX-xOff, event.clientY-yOff]
setBox();
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillRect(corner[0],corner[1],boxW,boxH);
//console.log(p2)
//console.log(corner[0]+" "+corner[1]+" "+boxW+" "+boxH);
}
});
canvas.addEventListener("mouseup", function finishBox(event) {
p2=[event.clientX-xOff, event.clientY-yOff];
setBox();
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillRect(corner[0],corner[1],boxW,boxH);
cropinit=false;
});
当我使用普通的JavaScript代码在**中设置画布宽度和高度时,该代码就可以工作了。但是,当我使用jQuery在带有注释的**中设置宽度和高度时。矩形不是在用户鼠标的起点和终点绘制的。jQuery和普通JS版本似乎产生了相同的画布宽度和高度,但矩形是在不同的地方绘制的。他们似乎做着完全相同的事情。有什么区别?
JavaScript element.width
和jQuery width()
是不等价的。
element.width
更改HTML属性element.style.width
更改CSS样式值jQuery width()
更改CSS样式值,与上一个样式值等效。
谈到canvas
,它的width
和height
属性定义了实际的绘图大小。同时,width
和height
CSS属性控制图像的比例。
进一步注意,
height
和width
是用于绘图的逻辑画布维度,并且不同于style.height
和style.width
CSS属性。如果不设置CSS属性,画布的内部大小将用作其显示大小;如果您设置了CSS属性,并且它们与画布维度不同,则您的内容将在浏览器中进行缩放。
有一篇很好的Stackoverflow文章,用实例解释了canvas
的width
和height
的含义。
jQuery width()结果
因此,正如我所说,jQuery width()
函数会更改对象的内联样式。
$("canvas").width(500).height(400);
document.body.innerText = document.getElementsByTagName("canvas")[0].outerHTML;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas></canvas>
element.width结果
然而,这个jQuery数组中的每个项都是一个DOM元素:
var canvas = $("#selector")[0]; // HTML element object
canvas.width = 500; // changing the HTML element object's properties
当您更改canvas
的height
和width
属性时,它实际上会更改其属性:
document.getElementsByTagName("canvas")[0].width = 500;
document.getElementsByTagName("canvas")[0].height = 400;
document.body.innerText = document.getElementsByTagName("canvas")[0].outerHTML;
<canvas></canvas>
结论
因此,在画布的情况下,您需要更改HTML属性,但不需要更改样式规则,而jQuery width()
和height()
函数不适合这样做
同时,您可以通过以下方式使用jQuery:
$("canvas").prop({"height": 500, "width": 400});
然而,它的作用与您的纯JS代码完全相同。
- Node.js使用Series函数(模式?)实现流控制时出现意外结果
- 在IntelliJ IDEA中,如何将JS文件从“;查找用法“;使用TypeScript时的结果
- 是否可以将JXBrowser显示的JS确认对话框的结果返回到调用它的JS部分
- Typeahead.js未显示异步结果
- async问题,JS Promise无法返回结果,但可以使用console.log
- Javascript验证(gen_validatorv4.js)-基于复选框结果
- MeteorJS从上一个函数中检索JS中的结果
- Meteor js-控制台显示'未定义'即使返回结果
- 在safari和chrome中,js图像加载产生了奇怪的结果
- 如何使用node.js和express在文件中写入结果
- Three.js CSG如何更改结果's的材质和颜色
- 无法使用streams/highland.js从mongodb的结果中获取数据
- 获取结果未定义(主干.js)
- Moment.js不显示结果
- 在Minitest中,我可以在JS页面上触发并分析结果/响应吗
- Angular JS根据搜索结果和点击事件更新DOM元素
- 使用 js 函数显示 HTML 表单的结果
- 在 sql 中求和 col,在 js 中查看结果
- 为什么这个js代码的RSA加密结果与java的RSA标准代码不同
- 如何在模态助推器中显示计算结果(JS)