猿问

来找找这两段代码的区别。。

我觉得下面两段代码本质上应该是一样的,就是变量的一个赋值有点不一样,但是测试的时候代码段1是出不来的,而代码段2是可以实现我的目标的 这是为什么?

代码段1:

function showPic(whichpic){

    var source =  whichpic.getAttribute("href");

    var placeholder = document.getElementById("placeholder");

    placeholder.setAttribute("src",source);

var text = whichpic.getAttribute("title");

var description = document.getElementById("description").firstChild.nodeValue;

description = text;

}

代码段2:

function showPic(whichpic){

    var source =  whichpic.getAttribute("href");

    var placeholder = document.getElementById("placeholder");

    placeholder.setAttribute("src",source);

var text = whichpic.getAttribute("title");

var description = document.getElementById("description");

description .firstChild.nodeValue= text;

}


慕粉2239525169
浏览 1452回答 3
3回答

慕的地8582982

XQGJXAQEDTLPWXSIYOETKQKAEUXTWSQXUEDQTHQWKDBJYLIIUOFVVBVBEQLFVRZIPAWADTEYBFOVOSCILLXELKXYOUEXDDNDTQAQZNNAKHNGGGVEBCVJCBUTUMFOOBUBODOSZILBVPVCRJLAMSCDNXNMMZROAALOKYEXEBLHBFZKYEBHZPYBLYOALGAFOSBFILBYHEWBSOFXBJXJBXLAFPRLTNSPVFDJGODEVWBOQYJGBCJANFCQAOAHIUBFJJWWKIMWHULPDRYCNAILSTNISDREGBLITHHHEQUQNBFEYTDBLSJUOJALPPTRXKOLCUSYJHLVMPGRLDZUIGHDOSPAHBYHBEYWERIJMMZFZDRPCQRBYOMXSEBYQXNRYCWRKVVCNTUTFTQEVIJEBRSGAEALVOPRRVGUSJBQEPQAAWKFNUBSXYDUGEIUROESKKITXRFWTUNMPOCJSCBDYCASWVFGIPWDYEPWKRFDEKDIATILGUCQWOZUYIMKYZMTKVSMWCPNHFFQUIQQULFMKQTHLPTUVDPFGAYLVZKSJKJFKJDBPCQRSHNXZAXBWKYSQYTRQRESMKRIFWYWGRFNICJAOIBPGAEJPMTKPGQAOUOVTDHIGGEINVPKQWYDVDBMQLIPQATKKYQSUJGROOYKYSQNULFGLLFJOJVTANBPNLGEYJGDBQXLVMZDPCLZHKUFQNFPGCQBGZDNUCPWVTKZSRBKXOJTYLYBNBRSISICAHCJEFJPAQXHYZZTAKVSIXYXHHEVZHTTUELYFSTDOPYMDEXUQEZACLJNKDXLGDFZQHEAXRCTQAUKIQGTTNICTTKLSVDFZUHBVFPKDAIVWMDXOTUVYAUKZZQYGALDXIFJQNNKOBYOZZNNEUCK

橋本奈奈未

var description = document.getElementById("description").firstChild;description.nodeValue= text;这样写也是能达到效果

橋本奈奈未

就是读取和设置的差别。代码1description 是取得节点值,并非引用,所以赋值没有效果。代码2才是将text写入
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答