js处理json以及字符串的比较等常用操作(2)


<html>
<head>
<title>IT-Homer demo</title>
</head>

<body>
<input type="button" value="parse json" />
<div />

<script type="text/javascript">
function parseJson222(){
var txt = document.getElementById("txt");

// var jsonData_old = '_2,4,_4,21,_5,22,8,_7,23,_9,11,12,13,61';
// var jsonData_new = '_2,_3,1,2,3,4,_4,21,_5,22,_6,5,6,7,8,_7,23,_9,11,12,13,14,-1,61';

var jsonData_new = '_2,4,_4,21,_5,22,8,_7,23,_9,11,12,13,61';
var jsonData_old = '_2,_3,1,2,3,4,_4,21,_5,22,_6,5,6,7,8,_7,23,_9,11,12,13,14,-1,61';

var diff = diffJson(jsonData_old, jsonData_new);

txt.innerHTML = diff;

printArray("diff", diff);
}

function diffJson(jsonData_old, jsonData_new){
var diff = "";

var oldArray = jsonData_old.split(",");
var newArray = jsonData_new.split(",");
var oldLen = oldArray.length;
var newLen = newArray.length;

var minLen = Math.min(oldLen, newLen);
if(minLen == newLen){
tmpArray = newArray; // swap array
newArray = oldArray;
oldArray = tmpArray;

newLen = oldLen; // swap array length
oldLen = minLen;
}

printArray("newArray", newArray);
printArray("oldArray", oldArray);

var arr = [];
for(i=0; i<newLen; i++){
var j=0;
for(j=0; j<oldLen; j++){
if(newArray[i] == oldArray[j]){
break;
}
}
if(j == oldLen){
arr.push(newArray[i]);
}
}
return arr;
}

function printArray(tag, arr){
var len = arr.length;
document.write("<br>");
document.write(tag + " : " + arr.toString());
}

</script>
</body>
</html>


运行结果:

js处理json以及字符串的比较等常用操作

 
Demo5:

复制代码 代码如下:


<html>
<head>
<title>hello</title>
<style type="text/css">
#adddelTextId{
float: clean;
}

normal {
font-style: normal;
color: #000000;
}

add {
font-style: normal;
color: #cc0000;
}

del {
font-style: normal;
color: #0000ff;
text-decoration: line-through;
}
</style>
</head>

<body>

<textarea cols="50" rows="5" >
</textarea><br />

<div>
<div>我,喜,欢</div>
<add><div>,你</div></add>
<del><div>,做,朋,友</div></del>
</div>

<input type="button" value="click me">

<script language="javascript">
var textareaId = document.getElementById("textareaId");

var adddelTextId = document.getElementById("adddelTextId");
var normalTextId = document.getElementById("normalTextId");
var addTextId = document.getElementById("addTextId");
var delTextId = document.getElementById("delTextId");

function initLoad(){
adddelText = normalTextId.innerHTML + addTextId.innerHTML;
textareaId.innerHTML = adddelText;
}

function hh(){
adddelTextFunc(true, ",IT-Homer");
adddelTextFunc(false, ",Sunboy_2050");
}

function adddelTextFunc(isAdd, txt){
if(isAdd){ // add
addTextId.innerHTML = txt;
} else { // del
delTextId.innerHTML = txt;
}

adddelText = normalTextId.innerHTML + addTextId.innerHTML;
textareaId.innerHTML = adddelText;
}
</script>

</body>
</html>


运行结果:

js处理json以及字符串的比较等常用操作

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdzfzy.html