[英]issues using .css() to style height
I'm doing a tutorial on basic jQuery plugins. 我正在做有关基本jQuery插件的教程。 I'm having a heck of a time trying to figure out why my code isn't altering the height of my
<p></p>
. 我花了点时间试图弄清楚为什么我的代码没有改变
<p></p>
的高度。
The exercise says to create a plugin that will set the height of the passed in elements without using .height()
. 练习说明创建一个插件,该插件将设置传入元素的高度,而无需使用
.height()
。
While the mini plugin below won't change the height, if I simply flip the height
property within .css()
to something like margin
it works perfectly. 虽然下面的迷你插件不会更改高度,但是如果我只是将
.css()
的height
属性翻转到margin
之类的话,它会完美地工作。
What am I missing? 我想念什么?
<body>
<p>Hello</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
(function($, window, document, undefined) {
$.fn.setHeight = function(hgt) {
return this.each(function() {
$(this).css('height', hgt);
});
};
})(jQuery, window, document);
</script>
<script>
$("p").setHeight(200)
</script>
</body>
There are two things that I would do here: 我在这里要做两件事:
First, I would make sure to explicitly set the display
property of the p
tag to display:inline-block
or display:block
, just in case this was modified elsewhere. 首先,我将确保将
p
标记的display
属性显式设置为display:inline-block
或display:block
,以防万一在其他地方对其进行了修改。
Second, and this is the more important part, change the height value, hgt
, that you pass to the plugin to be a string with a unit ( px
, pt
, em
, etc.) appended to it. 其次,这是更重要的部分,更改传递给插件的高度值
hgt
,使其成为附加了单位( px
, pt
, em
等)的字符串。
Good luck and let me know if you have any further questions/problems! 祝您好运,如果您还有其他疑问/问题,请告诉我! :)
:)
UPDATE UPDATE
Let's start by simplifying the plugin. 让我们从简化插件开始。 Try this and let me know what the result is:
试试这个,让我知道结果是什么:
(function($, window, document, undefined) {
$.fn.setHeight = function(hgt) {
$(this).css('height', hgt);
};
})(jQuery, window, document);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.