2010-08-09 75 views
0

我有以下的HTML原型刪除HTML?

<div id="top-right"> 
<span id="top-right-name">sometexthere</span> | <a href="#">link</a> 
</div> 

而下面的原型JS

Event.observe(window, 'load', function() { 
try { 
if ($$('#top-right')!=null) { 

var topmenu = document.getElementById('top-right'); 
var value = topmenu.innerHTML; 
// define an array of replacements 
var replacements = [ 
    { from: '|', to: ' ' }, 
    { from: '|', to: ' ' }, 
    { from: '|', to: ' ' }, 
    { from: '|', to: ' ' } 
]; 
for (var i = 0, replacement; i < replacements.length, replacement = replacements[i]; i++) { 
    // replace 
    value = value.replace(replacement.from, replacement.to); 
} 
// replace the old text with the new 
topmenu.innerHTML = value; 

} 
} 
catch(ex) { 
} 
}); 

我想刪除 「|」 的</SPAN後>自動的onload的這個JS - 但我只是似乎無法做到這一點。

有人可以協助嗎?

謝謝

回答

0

這似乎是一個語法錯誤的地方,也許在您的加載原型。以下代碼段的工作對我很好:)

<script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script> 

<div id="top-right"> 
<span id="top-right-name">sometexthere</span> | <a href="#">link</a> 
</div> 

<script type="text/javascript"> 
Event.observe(window, 'load', function() { 
try { 
if ($$('#top-right')!=null) { 

var topmenu = document.getElementById('top-right'); 
var value = topmenu.innerHTML; 
// define an array of replacements 
var replacements = [ 
    { from: '|', to: ' ' } 
]; 
for (var i = 0, replacement; i < replacements.length, replacement = replacements[i]; i++) { 
    // replace 
    value = value.replace(replacement.from, replacement.to); 
} 
// replace the old text with the new 
topmenu.innerHTML = value; 

} 
} 
catch(ex) { 
} 
}); 
</script> 

編輯,以反映問題

+0

喜肯尼 - 感謝您的幫助,但我使用Prototype庫 - 不是jQuery的 - 因此語法$$: ) – Tom 2010-08-09 17:39:28

+0

哦,廢話。對不起,我沒有注意到! 是至少被調用的Event.observe(window,'load',function(){})?除此之外,你的語法對我來說似乎很好! :) – 2010-08-09 17:48:58

+0

乾杯thx - 是加載問題:) – Tom 2010-08-09 18:09:32