【问题标题】:JQuery not appending div tag with attributes from a JS function objectJQuery 不使用 JS 函数对象的属性附加 div 标签
【发布时间】:2015-12-01 19:40:34
【问题描述】:

我正在尝试使用 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 附加块时,我都会得到以下输出:&lt;div class="undefined" style="width:undefinedpx;height:undefinedpx;"&gt;&lt;/div&gt;

非常感谢任何帮助。

此致,CA1K。

【问题讨论】:

    标签: javascript jquery html function oop


    【解决方案1】:

    你把它倒过来了。应该是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(因为没有定义任何属性)。

    【讨论】:

      猜你喜欢
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多