Javascript jQuery'中的不一致性;s属性操作 $(“某物”) .removeAttr('selected') .wrap(“

Javascript jQuery'中的不一致性;s属性操作 $(“某物”) .removeAttr('selected') .wrap(“,javascript,jquery,dom,attributes,option,Javascript,Jquery,Dom,Attributes,Option,”).parent().html(); 导致 $('<option selected="selected">something</option>') .removeAttr('selected') .wrap('<p></p>').parent().html(); 什么 这是意料之中的。但是,如果在删除“selected”属性后将其放回(或放回没有“selected”属性的标记),则会得到相同的输出 <option>somethi

”).parent().html(); 导致

$('<option selected="selected">something</option>')
.removeAttr('selected')
.wrap('<p></p>').parent().html();
什么 这是意料之中的。但是,如果在删除“selected”属性后将其放回(或放回没有“selected”属性的
标记),则会得到相同的输出

<option>something</option>
$(“某物”)
.removeAttr('selected')
.attr('selected'、'selected')
.wrap(“

”).parent().html();
为什么会发生这种情况?

首先,一个
不应该被包装在一个段落中-这可能是你看到的一些奇怪行为的原因。此外,“selected”属性可能不会在HTML标记中表示,但它是在其计数的位置设置的,即DOM“selected”属性

$('<option selected="selected">something</option>')
.removeAttr('selected')
.attr('selected', 'selected')
.wrap('<p></p>').parent().html();
var选项=$('something');
option.removeAttr('selected').attr('selected','selected');
选项[0]。所选===true;

选择了两个
属性,即
选择的
属性和
选择的
属性

$('<option selected="selected">something</option>')
.removeAttr('selected')
.attr('selected', 'selected')
.wrap('<p></p>').parent().html();
HTML
selected=“selected”
属性设置JavaScript
selected
boolean属性的初始状态,但之后,与所有表单字段状态一样,它们是独立的

如果通过在选择框中单击该选项来选择该选项,则不会将HTML
selected=“selected”
属性添加到HTMLOPIONElement DOM节点,因此您不会看到该属性反映在元素的
innerHTML
/
HTML()
中。相反,它只设置
选项所反映的基础表单内容。selected
属性

$('<option selected="selected">something</option>')
.removeAttr('selected')
.attr('selected', 'selected')
.wrap('<p></p>').parent().html();
IE 8版之前的版本甚至不允许您设置HTML
selected
属性,因为
setAttribute
在那里被破坏。如果您尝试,它实际上会设置
selected
boolean属性

$('<option selected="selected">something</option>')
.removeAttr('selected')
.attr('selected', 'selected')
.wrap('<p></p>').parent().html();

jQuery中的
attr()
是一种属性和属性访问的混合体,它试图掩盖这些问题。但是结果是调用
attr('selected')
实际上与使用布尔
selected
属性相同。

看到这个分析,可以在DOM中看到的是它被选中了项,但是HTML的文本看不到

$('<option selected="selected">something</option>')
.removeAttr('selected')
.attr('selected', 'selected')
.wrap('<p></p>').parent().html();
var option = $('<option selected="selected">something</option>');
option.removeAttr('selected').attr('selected', 'selected');

option[0].selected === true;

$(函数(){
var opc0=document.createElement(“选项”);
var theText=document.createTextNode(“0”);
opc0.appendChild(文本)
opc0.setAttribute(“选定”、“选定”);
opc0.删除属性(“选定”);
opc0.setAttribute(“选定”、“选定”);
var opc1=$(“1”);
opc1.移除标签(“标签”);
opc1.删除注释(“选定”);
opc1.attr(“选定”、“选定”);
opc1.attr(“标签”、“标签2”);
var opc2=$(“2”);
opc2.删除注释(“选定”)
变量opc3=$(“”);
var opc3a=$(“3A”);
var opc3b=$(“3B”);
var opc3c=$(“3c”);
var opc3d=$(“3d”);
var opc3e=$(“3e”);
var opc3f=$(“3f”);
opc3.append(opc3a);
opc3.append(opc3b);
opc3.append(opc3c);
opc3.append(opc3d);
opc3.append(opc3e);
opc3.append(opc3f);
opc3a.删除注释(“选定”);
opc3.val(“a”);
var html0=opc0.outerHTML;
var html1=opc1.parent().html();
var html2=opc2.parent().html();
var html3=opc3.parent().html();
$(“#preTest0”).text(html0);//确定
$(“#preTest1”).text(html1);//错误
$(“#preTest2”).text(html2);//确定
$(“#preTest3”).text(html3);//错误
$(document.body)
.附加(opc1)
.附加(opc3);
$(“#selectTest option:eq(4)”).attr(“selected”、“selected”);
$(“#selectTest option:selected”).each(函数(){$(“#preTest5”).text($(“#preTest5”).text()+$(此).text()+“已选中”););
var html4=opc3.html();
$(“#preTest4”).text(html4);//ok accion,err html视图
});







使用jQuery 1.3.2和IE 7进行了尝试。所选属性如预期的那样存在……但显然不是在FireFox 3.5和Google Chrome中。有趣的…我可以问一下为什么不应该在段落中包装?我在
中包装了
标记只是为了测试序列化行为。