JS删掉字符串中重复字符方法
发布时间:2023-10-25 14:51:08 所属栏目:教程 来源:
导读:代码如下:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
代码如下: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var str=$("#org").val(); var arr=str.split(""); for(var i=0;arr.length-1>i;i++){ for(var j=i+1;j<arr.length;j++){ if(arr[j]==arr[i]){ alert(arr.length); arr.splice(j,1); //删除之后,数组长度随之减少 j--; } } } alert(arr.toString()); //$("#dl").val(arr.toString());//生成的字符串由逗号隔开 $("#dl").val(arr.join(""));//生成的字符串无分割符 $("#dl").val(arr.join("-"));//join方法可指定生成的字符串的分隔符 $("#dl").attr({"style":""}); }) }); </script> </head> <body> <input id="org" type="text" value="hhuummqqhhuummss"/> <button type="button">去除重复</button> <input id="dl" type="text" style="display:none"/> </body> </html> (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐