Javascript 构造函数中的px未在html中显示
我试图通过JS构造函数和jquery代码将div添加到html中 像素元素,即使已定义,似乎也会被html忽略 请问,你能告诉我哪里错了吗Javascript 构造函数中的px未在html中显示,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我试图通过JS构造函数和jquery代码将div添加到html中 像素元素,即使已定义,似乎也会被html忽略 请问,你能告诉我哪里错了吗 function Square(width, height){ this.width = width +"px"; this.height = height +"px"; this.background = "pink"; } var secondSquare = new Square (100,100, "pink"); $(".btn-
function Square(width, height){
this.width = width +"px";
this.height = height +"px";
this.background = "pink";
}
var secondSquare = new Square (100,100, "pink");
$(".btn-new-square").on('click',function() {
// console.log("pink sq");
$("body").append('<div id="newSquare">sdfghjkjhgf</div>');
// console.log('newSquare');
//$("#newSquare").append(secondSquare);
//$("#newSquare").addClass('newSquare');
console.log(secondSquare.height);
console.log(secondSquare.width);
console.log(secondSquare.background);
$("#newSquare").css({
"heigth": secondSquare.heigth,
"width": secondSquare.width,
"background-color": secondSquare.background });
});
功能方块(宽度、高度){
this.width=宽度+px;
this.height=高度+px;
this.background=“pink”;
}
var secondSquare=新正方形(100100,“粉色”);
$(“.btn新方块”)。在('click',function()上{
//控制台日志(“粉色sq”);
$(“正文”).append('sdfghjkjhgf');
//console.log('newSquare');
//$(“#新闻广场”)。附加(第二广场);
//$(“#newSquare”).addClass('newSquare');
控制台。原木(第二平方米。高度);
控制台。原木(第二方形。宽度);
console.log(secondSquare.background);
$(“#新闻广场”).css({
“高”:第二个正方形。高,
“宽度”:secondSquare.width,
“背景色”:secondSquare.background});
});
您在此处拼写的高度错误:高度:secondSquare.heigth
在属性名和属性名中。请尝试此操作
function Square(width, height,color){
this.width = width +"px";
this.height = height +"px";
this.background = color;
}
var secondSquare = new Square (100,100, "pink");
$(".btn-new-square").on('click',function() {
$("body").append('<div id="newSquare">sdfghjkjhgf</div>');
console.log(secondSquare.height);
console.log(secondSquare.width);
console.log(secondSquare.background);
$("#newSquare").css({
"height": secondSquare.height,
"width": secondSquare.width,
"background-color": secondSquare.background });
});
功能方块(宽度、高度、颜色){
this.width=宽度+px;
this.height=高度+px;
这个背景=颜色;
}
var secondSquare=新正方形(100100,“粉色”);
$(“.btn新方块”)。在('click',function()上{
$(“正文”).append('sdfghjkjhgf');
控制台。原木(第二平方米。高度);
控制台。原木(第二方形。宽度);
console.log(secondSquare.background);
$(“#新闻广场”).css({
“高度”:平方秒高度,
“宽度”:secondSquare.width,
“背景色”:secondSquare.background});
});
function Square(宽度、高度、颜色)//现在我在这里添加了3个参数
“height”:secondSquare.height,//height拼写错误
这对我来说很好。请检查这里的高度是
高度,而不是高度
。如果您是第二次说,那么它将不起作用,因为您使用的是Id$(“#newSquare”)。css()我想您不需要指定px,只需将数字作为数字而不是字符串。像css({width:50})