我正在尝试使用自定义 html5 验证动态添加输入。 jQuery代码如下。
$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\d*" title="Please Enter Valid No.">');
但是在 html 输出中看起来像这样:
<input type="text" name="test_input" id="test_input" required pattern="d*" title="Please Enter Valid No.">
而不是\d*
,而是d*
。
最佳答案
转义斜杠\
:
$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\\d*" title="Please Enter Valid No.">');
或者您也可以为\
编写字符代码,即\
,例如:
$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\d*" title="Please Enter Valid No.">');
关于jQuery 添加带有反斜杠的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30023569/