我打算在div中放置一个文本(垂直)(应该是中心,位于黑色区域[在border-left])。但是我在它们的结构中没有这个类名的某些页面中得到了这个错误(TypeError:变量是未定义的),换句话说,这个div.class不存在于它们上。这个代码在没有这个类名的页面中工作得很好任何错误。我该怎么办?
HTML:
<div class="container" style="margin-top:80px;">
<div class="row">
<div class="tree clearfix">
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
<div class="tree-content WhiteSkin tab-content">
<div class="tree-pane tab-pane fade" id="A">
<p>
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
</p>
</div>
<div class="tree-pane tab-pane fade in active" id="B">
<p>
Lorem ipsum, Founder and Director at Company.
</p>
</div>
<div class="tree-pane tab-pane fade" id="C">
<p>
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
</p>
</div>
<div class="tree-pane tab-pane fade" id="D">
<p>
Lorem ipsum, Founder and Director at Company.
John Doe, Director at Company.
John Doe, Director at Company.
</p>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<div class="tree-panel panel-container">
<ul>
<a href="#A" data-toggle="tab">
<li>A</li>
</a>
<a href="#B" data-toggle="tab">
<li class="active">B</li>
</a>
<a href="#C" data-toggle="tab">
<li>C</li>
</a>
<a href="#D" data-toggle="tab">
<li>D</li>
</a>
</ul>
</div>
</div>
</div>
</div>
</div>
Jquery的:
$(window).load(function(){
if (! $('.tree').get(0) ) return;
$('<div class="tree-title" />').appendTo('.tree-content');
$('.tree-title').text("Tree").css({
"position": "absolute",
"top": "0",
"left": "0",
"color": "#FFFFFF",
"font-size": "18px",
"-webkit-transform": "rotate(-90deg)",
"-moz-transform": "rotate(-90deg)",
"-o-transform": "rotate(-90deg)",
"transform": "rotate(-90deg)",
"z-index": "600"
});
});
(function($){
$.fn.tree = function (){
setTimeout(function()
{
var heightPane = $('.tree-pane.active').css('height');
heightPane = parseInt(heightPane);
var p = $('.tree-title').position();
var h = $('.tree-title').height();
var w = $('.tree-title').width();
var cal_1 = ((40 - h) / 2) - 1;
var cal_2 = (heightPane - w) / 2;
var _cal_1 = - ((p.top) - cal_2);
var _cal_2 = - ((p.left) - cal_1);
$('.tree-title').css({
"margin-top": _cal_1,
"margin-left": _cal_2
});
}, 200 + 20);
};
})(jQuery);
$(document).ready(function(){
$('.tree').tree();
$('.tree-panel a').bind("click",function(){
$('.tree').tree();
});
});
答案 0 :(得分:0)
要判断变量是否未定义,您可以使用
if (typeof myVariable == "undefined")
由于您使用的是jQuery,要验证某个类是否存在(以及是否存在),请使用
if ($(".myClass").length == 0) {
//stuff if class is absent
} else {
//your current instructions
}
编辑:在您的情况下,如果您不希望在缺少必需的课程时执行该代码,您可以执行this
之类的操作