JS字符串截取action=
时间: 2023-10-20 10:06:37 浏览: 50
你可以使用JavaScript中的字符串截取方法substr()或substring()来截取字符串中的action=部分。这两个方法的使用方式如下:
1. 使用substr()方法:
```
var str = "http://example.com/index.php?action=login";
var action = str.substr(str.indexOf("action=") + 7);
console.log(action); // 输出 "login"
```
2. 使用substring()方法:
```
var str = "http://example.com/index.php?action=login";
var start = str.indexOf("action=") + 7;
var end = str.indexOf("&", start) > -1 ? str.indexOf("&", start) : str.length;
var action = str.substring(start, end);
console.log(action); // 输出 "login"
```
这两种方法都能够截取字符串中的action=部分,并且将其赋值给变量action。需要注意的是,如果字符串中还有其他参数,那么第二种方法需要对结束位置进行判断,以避免截取到其他参数的值。
相关问题
action="javascript:;"
This is a JavaScript code snippet that sets the value of the "action" attribute of an HTML form to an empty JavaScript function. This is often used in situations where the form submission needs to be handled by JavaScript code rather than by a server-side script. The empty function can be replaced with a function that performs the desired actions when the form is submitted.
action=store_true
action=store_true是argparse模块中的一个参数,用于解析命令行参数时的一种行为。当使用action=store_true时,如果命令行中存在该参数,则将其值设置为True;如果命令行中没有该参数,则将其值设置为False。
这种行为通常用于解析布尔类型的命令行参数,例如:
```
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
args = parser.parse_args()
if args.verbose:
print("Verbose mode is on")
else:
print("Verbose mode is off")
```
在上述代码中,--verbose是一个布尔类型的命令行参数。如果在命令行中使用了--verbose参数,则args.verbose的值将被设置为True,否则为False。根据args.verbose的值,可以执行相应的操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)