将var附加到div

时间:2017-01-15 10:14:05

标签: javascript

我制作了一个轮盘游戏,但现在我在附加方法中使用了一个int。我想使用var数作为附加div的段落。提前谢谢!

if (Red == true) {

  Red = false;
  var randnum = Math.floor((Math.random() * 4) + 1);
  var number;
  switch (randnum) {
    case 1:
      number = 1;
      break;
    case 2:
      number = 3;
      break;
    case 3:
      number = 5;
      break;
    case 4:
      number = 7;
      break;

  }


$("#WheelList").append("<li><div class='red'><center>'number'</center></div></li>");

3 个答案:

答案 0 :(得分:1)

使用+ symbol简单地将数字与字符串连接起来。

private BeaconManager beaconManager =null;

private static final String ALTBEACON_LAYOUT = "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";

private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_beacon);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can detect beacons.");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                }
            });
            builder.show();
        }
    }

    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(ALTBEACON_LAYOUT));
    beaconManager.setForegroundScanPeriod(30000l);
    beaconManager.setForegroundBetweenScanPeriod(21000l);

    beaconManager.bind(this);

}

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[],
                                       int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_COARSE_LOCATION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "coarse location permission granted");
            } else {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Functionality limited");
                builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                    }
                });
                builder.show();
            }
            return;
        }
    }
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");
        }

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
        }
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:1)

这很容易:

$("#WheelList").append("<li><div class='red'><center>" + number + "</center></div></li>"); 
//                                          ---------^^^^^----^^^^^^----

答案 2 :(得分:0)

像这样。在javascript print(url)运算符用于连接。

fatal error: unexpectedly found nil while unwrapping an Optional value