空变量的jQuery检查
因此,我有以下js标记:空变量的jQuery检查,jquery,Jquery,因此,我有以下js标记: jQuery(document).on( 'click', '.rhpcon', function(e) { var rhp_name = jQuery(this).find('.rhi').data("name"); var my_html = '<div class="rhfi">'+ rhp_name +'</div>'; my_html += '<div class="something"&g
jQuery(document).on( 'click', '.rhpcon', function(e) {
var rhp_name = jQuery(this).find('.rhi').data("name");
var my_html = '<div class="rhfi">'+ rhp_name +'</div>';
my_html += '<div class="something">Show something else</div>';
jQuery('.rh_pop').html(my_html);
});
如您所见,rhp_name变量用于硬编码html
但是,在某些情况下,数据名不存在,即没有rhp_名称变量
在没有rhp_name变量的情况下,我不希望使用my_html的第一行
如何检查var是否为空等等
谢谢
if(typeof rhp_name !== 'undefined' && rhp_name != ''){
如果未设置属性,则应根据文档执行此操作。数据返回空字符串。那么你可以试试:
jQuery(document).on( 'click', '.rhpcon', function(e) {
var rhp_name = jQuery(this).find('.rhi').data("name");
if(rhp_name!=""){
var my_html = '<div class="rhfi">'+ rhp_name +'</div>';
my_html += '<div class="something">Show something else</div>';
jQuery('.rh_pop').html(my_html);
}
});
if (rhp_name.length > 0)
console.log("rhp_name exists");