gpt4 book ai didi

javascript - 原型(prototype)查找类并替换文本

转载 作者:行者123 更新时间:2023-11-28 02:57:01 25 4
gpt4 key购买 nike

我正在使用原型(prototype)框架[平台要求],我试图找到一个ID并替换ID中的一些文本以使其消失。

HTML 看起来像:

<div id="top">
<a href="/login">login</a> | <a href="/register">register</a>
</div>

问题是我不希望“|”出现在ID“top”中。所以我想这有点像“查找元素” | “在 ID 顶部并删除”

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

var topmenu = document.getElementById('top');
var value = topmenu.value;
// 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.value = value;
} }
catch(ex) {
}
});

尝试了上面的方法,但不起作用。有人可以帮忙吗?

谢谢

最佳答案

你不能用CSS来隐藏管道吗?只需更改颜色以匹配背景颜色,使其不可见。

#top {color: #fff } /* substitute with the right background color if different */
#top a {color: #000 } /* or whatever it has to be */

编辑:您应该替换的正确属性是innerHTML,而不是value

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;

关于javascript - 原型(prototype)查找类并替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com