JQuery - 同时设置css和实际宽度/高度宽度一个函数

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

本文关键字:高度 函数 一个 css 设置 JQuery      更新时间:2023-09-26
$("#elem").width(value) //This sets the "CSS" width of elem
$("#elem").attr("width", value) // This sets the "REAL" width of elem

现在,我的问题是,没有任何功能可以同时设置"REAL"和"CSS"宽度吗?

一个新的jQuery函数怎么样:

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

$(elem(.fixWidths(width(;

为什么不写一个呢?

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

尝试以下操作:

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";    

我希望它有所帮助。