如果不存在该如何创建数组并在创建数组后运行jquery函数? [英] How can i create an array if it doesn't exist and run a jquery function after it creates an array?

查看:84
本文介绍了如果不存在该如何创建数组并在创建数组后运行jquery函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小提琴.

我希望能够检查数组是否存在.如果它不存在,我需要创建它并运行一个this函数.

I want to be able to check if an array exists. If it does not exist i need to create it and run a this function.

$('p').live('click', function(){
    $('p').html('yay, it worked! Thankyou!')
}  

那行不通,我知道,但是我不知道如何检查.

That does not work and i know that but i do not know how to check it.

推荐答案

使用普通的if语句代替条件运算符:

Use a normal if statement instead of the conditional operator:

if(!$.isArray(x)) {
    x = [];
    $('p').live('click', function(){
        $('p').html('yay, it worked! Thankyou!')
    });
}

注意:必须至少声明x(var x;)才能起作用.

Note: x must be at least declared (var x;) for this to work.

参考: if...else jQuery.isArray

更新:阅读其他评论(现已删除的答案),您打算这样做吗?

Update: Reading the comments on the other (now deleted answer), did you intend to do something like this?

function doStuff() {
    $('p').html('yay, it worked! Thankyou!');
}

$('p').live('click', doStuff);

if(!$.isArray(x)) {
    x = [];
    doStuff();
}

鉴于您提供的信息,目前还不清楚.您刚刚说过运行此功能并发布了一个代码段,该代码段假定整个代码段都是您要在创建数组时运行的代码.请更精确.

It's not really clear given the information you provided. You just said run this function and posted a snippet which lets assume that the whole snippet is the code you want to run when the array is created. Please be more precise.

这篇关于如果不存在该如何创建数组并在创建数组后运行jquery函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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