JavaScript替换HTML不能正常工作的文本?

时间:2015-12-02 22:11:12

标签: javascript html

因此,我正在尝试让我的网站查找某些文字,并使用此代码将其替换为<img>标记...

function Emote() {
    var stringg = document.getElementsByClassName("Post").innerHTML;
    var resolutionn = stringg.replace("xD", "<img src='Icons/xD.gif' width='100px' height='100px' />")
    document.getElementsByClassName("demo").innerHTML = resolutionn;
}
Emote();

当我使用Firebug进行调试时,它会输出...

enter image description here

2 个答案:

答案 0 :(得分:3)

函数> listColumns(t2) [1] "* b (character)\n" "* c (numeric)\n" "* d (numeric)\n" "* e (factor)\n" 返回元素的数组,而不是单个元素。数组没有属性getElementsByClassName,因此未定义。

如果要将此函数应用于与该类匹配的第一个元素,请使用:

innerHTML

代替。

答案 1 :(得分:1)

int setAssoc(int associativity, vector<pair<unsigned long long, int>>& memAccess){ int blockNum, setNum; int hitRate = 0; int numOfSets = 16384 / (associativity * 32); int cache [numOfSets][associativity];//used to store blocks int age [numOfSets][associativity];//used to store ages int maxAge = 0; int hit;//use this to signal a hit in the cache //set up cache here for(int i = 0; i < numOfSets; i++){ for(int j = 0; j < associativity; j++){ cache[i][j] = -1;//initialize all blocks to -1 age[i][j] = 0;//initialize all ages to 0 }//end for int j }//end for int i for(int i = 0; i < memAccess.size(); i++){ blockNum = int ((memAccess[i].first) / 32); setNum = blockNum % numOfSets; hit = 0; for(int j = 0; j < associativity; j++){ age[setNum][j]++;//age each entry in the cache if(cache[setNum][j] == blockNum){ hitRate++;//increment hitRate if block is in cache age[setNum][j] = 0;//reset age of block since it was just accessed hit = 1; }//end if }//end for int j if(!hit){ for(int j = 0; j < associativity; j++){ //loop to find the least recently used block if(age[setNum][j] > maxAge){ maxAge = j; }//end if }//end for int j cache[setNum][maxAge] = blockNum; age[setNum][maxAge] = 0; } }//end for int i return hitRate; }//end setAssoc function 的返回值为document.getElementsByClassName("Post").innerHTML

那是因为undefined正在回退一个数组,而不是一个元素。

使用document.getElementsByClassName访问第一个:

document.getElementsByClassName("Post")[0].innerHTML