簡體   English   中英

jQuery不會在div標簽上附加來自JS函數對象的屬性

[英]JQuery not appending div tag with attributes from a JS function object

我正在嘗試使用JQuery在瀏覽器窗口上打印出一個塊。 我通過函數將塊屬性制成了JS對象類。 在這段代碼中,我使用的是"use strict"; 方法。

HTML(5):

<!DOCTYPE html>
<html><TITLE>Logo Experiment</TITLE>

<head>
<!--adding JQuery library-->
<!--this is a DESKTOP JQuery, so this will not work much on the modern androids-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!--setting charset to utf-8 unicode in case symbols are needed-->
<meta charset="utf-8">
</head>

<style>
body {
display: block;
}
.bx { /* the box... */
background-image: url("metal.png");
background-size: cover;
background-color: #333333; /* in case the image fails to load */
border-width: 1.5px;
border-radius: 6px;
border-style: solid;
border-color: #222222;
box-shadow: 2px 2px 2px #111111;
}
</style>

<body>

<script>
//the script below
</script>

</body>

</html>

JS / JQuery:

"use strict";

function Box(width, height, style) {//box object
width = this.width;
height = this.height;
style = this.style;
};

var lbx = new Box(256, 256, "bx"); //"lbx" is "logo box"

$(document).ready(function(){//appending a div to the <body> tag directly
$("<div class='" + lbx.style + "' style='width:" + lbx.width + "px;height:" + lbx.height + "px;'>").appendTo("body");
});

每當我嘗試使用JS對象屬性使用JQuery附加塊時,都會得到以下輸出: <div class="undefined" style="width:undefinedpx;height:undefinedpx;"></div>

任何幫助深表感謝。

真誠的,CA1K。

你在這里倒退了。 應該是this.parameter = paramter

function Box(width, height, style) {//box object
width = this.width;
height = this.height;
style = this.style;
};

像這樣:

function Box(width, height, style) {//box object
    this.width = width;
    this.height = height;
    this.style = style;
    };

第一種方法除了將調用參數設置為undefined之外沒有做其他事情(因為其中沒有定義任何屬性)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM