js去除字符串中所有html標簽及 符號


 

近日在做項目的時候,經常會在頁面上處理一些數據。結果發現自己js掌握的並不是很好。那就在這里記錄js的點點滴滴吧。

 

1. 去除字符串中的 html 標簽

function delHtmlTag(str){
  return str.replace(/<[^>]+>/g,"");
}
var str = "<span style='display:none;'>This is test</span><br/>";
str = delHtmlTag(str);
alert(str);

2. 去除字符串中的 &nbsp 空格符號

var test = "this &nbsp; is &nbsp; a test";
test = test.replace(/&nbsp;/ig, "");
alert(test);

3. js 指定周期調用函數

var count = 0;
var timePromise = window.setInterval(function(){
    count++;
    alert(count);
    if (count == 100) {
        // 停止
        window.clearInterval(timePromise);
    }
},1000);

4. windos 方法

// 刷新頁面
window.location.reload();
// 跳轉頁面
window.location=url;
window.location.href=url;
// 父頁面刷新 一般由window.open()打開的頁面的父頁面
window.opener.reloadGrid();
// 打開一個新頁面加載
window.open(url);

5. disabled 屬性的使用

// 兩種方法設置disabled屬性
$('#areaSelect').attr("disabled",true);
$('#areaSelect').attr("disabled","disabled");

// 三種方法移除disabled屬性
$('#areaSelect').attr("disabled",false);
$('#areaSelect').removeAttr("disabled");
$('#areaSelect').attr("disabled","");

 6. display 隱藏元素

$(obj).css("display","none");

 7. 獲取地址欄參數

function getParams(name){
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if(r!=null)return  unescape(r[2]); return null;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM