简体   繁体   English

jQuery-同时设置CSS和实际宽度/高度宽度

[英]JQuery - Set both css and real width/height width one function

$("#elem").width(value) //This sets the "CSS" width of elem

$("#elem").attr("width", value) // This sets the "REAL" width of elem

Now, my question is, isn't there any function that sets both the "REAL" and "CSS" width at the same time? 现在,我的问题是, 是否没有同时设置“ REAL”和“ CSS”宽度的函数?

How about a new jQuery function: 新的jQuery函数如何:

$.fn.fixWidths = function(width) {
  return $(this).width(width).attr('width', width);
};

$(elem).fixWidths(width); $(elem).fixWidths(width);

Why not write one? 为什么不写一个?

function setWidths(id, width){
    $('#' + id).width(width);
    $('#' + id).attr('width', width);
}

try following: 尝试以下操作:

var canvas = document.createElement("canvas");

// this sets canvas tag attributes
canvas.width = width;
canvas.height = height ;

// this sets style of canvas
canvas.style.width = width + "px";
canvas.style.height = height + "px";    

i hope it helps. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM