2015-09-05 53 views
0

我試圖用JQuery打印出一個塊到瀏覽器窗口。我通過函數將塊屬性變成JS對象類。在這段代碼中我使用了"use strict";方法。JQuery不附加div標籤與來自JS函數對象的屬性

的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。

回答

2

你在這裏倒退了。它應該是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; 
    }; 

第一種方式做無非是調用的參數設置爲未定義其他(因爲沒有屬性有定義)。

相關問題