我有一个javascript playAudio()函数,它需要一个字符串参数。我似乎无法弄清楚如何将字符串值作为此函数的参数传递而不会破坏字符串。
htmlString = "<button class='button-icon' value='PLAY' onclick='playAudio()'></button>";
我想传递字符串&#34; SP00005&#34;作为参数。但是我无法使用引号或撇号,因为这些已用于我的htmlString的更外部。
我甚至尝试将htmlString的形成分解为各个步骤,如下所示:
htmlString = "";
htmlString += "<button class='button-icon' value='PLAY' onclick='playAudio(";
htmlString += "SP00005";
htmlString += ")'></button>";
所以我的问题是:如何在一个字符串中传递字符串,该字符串本身就是一个字符串?
答案 0 :(得分:2)
一种方法是逃避"
。
htmlString = "<button class='button-icon' value='PLAY' onclick='playAudio(\"foo\")'></button>";
更好的选择是选择带有javascript的按钮,添加一个eventListener并且不要担心内联js
<button id="fooBtn" class='button-icon' value='PLAY'></button>
JS:
document.getElementById('fooBtn').addEventListener('click', function() {
playAudio("SP00005");
});
答案 1 :(得分:0)
您可以使用\"'
示例测试= "Hello, I am \"Steve\""
解析为:你好,我是“史蒂夫”
答案 2 :(得分:0)
你需要这样做,使用\&#34;在playAudio()中:
htmlString = "";
htmlString += "<button class='button-icon' value='PLAY' onclick='playAudio(\"";
htmlString += "SP00005";
htmlString += "\")'></button>";
答案 3 :(得分:0)
您也可以使用后退。这将允许您根据需要构建字符串而不使用转义字符
package com.lab.rafael.smartattendance;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.BeepManager;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import java.util.List;
public class TakeAttendanceFragment extends Fragment {
private final int CAMERA_PERM_REQUEST = 0;
private static final String TAG = TakeAttendanceFragment.class.getSimpleName();
DecoratedBarcodeView barcodeView = null;
BeepManager beepManager = null;
String lastText;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_take_attendance, container, false);
}
@Override
public void onStart() {
super.onStart();
if(getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.CAMERA}, CAMERA_PERM_REQUEST);
} else {
startCamera();
}
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode == CAMERA_PERM_REQUEST) {
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
}
}
}
private void startCamera() {
try {
if(getView() != null) {
barcodeView = (DecoratedBarcodeView) getView().findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(barcodeCallback);
beepManager = new BeepManager(getActivity());
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
private BarcodeCallback barcodeCallback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if(result.getText() == null || result.getText().equals(lastText)) return;
lastText = result.getText();
barcodeView.setStatusText(lastText);
beepManager.playBeepSoundAndVibrate();
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
public void onResume()
{
super.onResume();
barcodeView.resume();
}
public void onPause() {
super.onPause();
barcodeView.pause();
}
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals