js return to state control select the previously selected

For a long time, there should be relatively simple, not more than that, on the code

page:

<div id="testArea">
<select class="selq" onchange="startchosee(this)" >
<option value="1">选择1</option>
<option value="2">选择2</option>
<option value="3">选择3</option>
<option value="4">选择4</option>
<option value="5">选择5</option>

</select>

</div>

js control:

var thisElement=$("#testArea").html();
var beforeElement="";

 

function startchosee(obj){


if(confirm("是否改变"+$(obj).find("option:selected").html())){

}else{
if(beforeElement!=""){
$("#testArea .selq").remove();
$("#testArea").append(parseDom(beforeElement));
}
}
beforeElement=nodeToString(obj);
thisElement=obj;
}

 

As for why do the two converters, because jquery source lack a deep understanding
// String into dom
function parseDOM (Arg) {

   var objE = document.createElement("div");

   objE.innerHTML = arg;

   return objE.childNodes;

};
// dom turn into String

function nodeToString ( node ) {

console.log($(node).find("option:selected").val())
var tmpNode = document.createElement( "div" );
tmpNode.appendChild( node.cloneNode( true ) );
$(tmpNode).find("option").each(function(){
$(this).attr("selected",null);
});
$(tmpNode).find("option:eq("+($(node).find("option:selected").val()-1)+")").attr("selected",true);
var str = tmpNode.innerHTML;
tmpNode = node = null; // prevent memory leaks in IE
console.log(str)
return str;
}

 

The main borrow someone else's code. Slightly rough. . Forgive me

 

Guess you like

Origin www.cnblogs.com/xy-dsh/p/11441981.html