是变量集/定义的问题? [英] Is variable set/defined issue?

查看:181
本文介绍了是变量集/定义的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个论坛软件,您可以使用以下命令来提取头像链接:{$ mybb-> user ['avatar']}



如果否头像设置,然后没有返回。我想在没有设置头像的情况下显示图片。



目前HTML的设置是这样的:

 < div id =avatar>< div style =background:url({$ mybb-> user ['avatar']})center center no-repeat; >< / DIV>< / DIV> 

外部div是空的,并且在后台。这也是我添加一个类的人(.defaultavatar),以便显示一个默认的头像。



内部div是显示头像的人。



这是我使用的javascript:

  var myavatar = {$ mybb-> user ['avatar']}; 

if(typeof(myavatar)= null){
jQuery('#avatar')。addClass('defaultavatar');
}

如果头像网址为空,我希望将类defaultavatar添加到第一个div。



我该怎么做(让它工作)?

非常感谢,
Jack Clarke

解决方案

undefined 中有两个含义JavaScript的。我不确定他们的确切名称是什么,因为我的例子我会称它们为undefined,并且是未定义的。


$ b 常规 undefined 是当你试图从已声明但未初始化的东西获得一个值时得到的结果。换句话说,解释者知道你在说什么,但那件事没有价值。

你可以使用一个简单的if语句将你的变量转换为布尔值来检查它是否是这种类型的 undefined 。请注意,这种转换也会将空字符串''和数字 0 转换为 false 以及。如果您需要考虑空字符串和数字 0 ,请使用 myDeclaredVariable === undefined 来代替条件。

  if(myDeclaredVariable){
// myDeclaredVariable使用truthy值初始化
} else {
// myDeclaredVariable要么是未初始化的,要么是假值
}

真正的 undefined 就是你在尝试访问一个随机变量时甚至没有声明它的时候得到的结果。解释器不知道你在说什么,如果你试图用它做任何事情,它会抛出一个javascript异常。要处理这种情况,您需要使用 typeof operator

  if(typeof myPossiblyDeclaredVariable!=='undefined'){
// myPossiblyDeclaredVariable已被声明为
} else {
// myPossiblyDeclaredVariable尚未被声明
}

你可以看到一个相关的StackOverflow问题这里


I'm using a forum software where you can pull up an avatar link using this: {$mybb->user['avatar']}

If no avatar is set then nothing is returned. I want to show an image if no avatar is set.

Currently the HTML is setup like this:

    <div id="avatar"><div style="background: url({$mybb->user['avatar']}) center center no-repeat;"></div></div>

The outer div is empty and in the background. This one is the one I'm adding a class too (.defaultavatar) so that a default avatar will display.

The inner div is the one with the avatar displaying. If no avatar is uploaded/selected then it will appear blank.

This is the javascript I'm using:

    var myavatar = {$mybb->user['avatar']};

    if(typeof(myavatar) = null){ 
        jQuery('#avatar').addClass('defaultavatar');
    }

If the avatar URL is empty I want the class defaultavatar to be added to the first div.

How can I do this (get it to work)?

Thanks very much, Jack Clarke

解决方案

There are two meanings for undefined in javascript. I'm not sure what their exact names are, for my example I will call them undefined and REALLY undefined.

The regular undefined is what you get when you try to get a value from something that has been declared but not initialized. In other words, the interpreter knows the thing you're talking about, but that thing has no value yet.

You can use a simple if statement to cast your variable to a boolean to check if it is this type of undefined. Be aware that this cast will also cast empty strings '' and numeric 0 to false as well. If you need to consider empty strings and numeric 0 as well, use myDeclaredVariable === undefined for the condition instead.

if (myDeclaredVariable) {
    // myDeclaredVariable is initialized with a "truthy" value
} else {
    // myDeclaredVariable is either uninitialized or has a "falsey" value
}

The REALLY undefined is what you get when you try to access a random variable without ever even declaring it. The interpreter has no idea what you are talking about and it will throw a javascript exception if you try to do anything with it. To handle this situation, you need to use the typeof operator.

if (typeof myPossiblyDeclaredVariable !== 'undefined') {
    // myPossiblyDeclaredVariable has been declared
} else {
    // myPossiblyDeclaredVariable has not been declared yet
}

You can see a related StackOverflow question here.

这篇关于是变量集/定义的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆