Javascript - 链接名称更改与限制 [英] Javascript - Link Name Changing with restrictions

查看:84
本文介绍了Javascript - 链接名称更改与限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改链接的名称,但是,我有一些限制。链接放置在代码中,如下所示:

 < li class ='time'> 
< a href =#time>查看时间< / a>
< img alt =Styledsrc =blah/>
< / li>

基本上,我有一个类名可以使用。我不允许在这些行中编辑任何内容,而且我只有一个页眉/页脚来编写JavaScript / CSS。我试图让时间显示为<例如,时间回顾

我知道我可以通过在CSS中使用.time {display:hide}来隐藏它,但我无法找到替换文本的方法。如图所示,文本也是一个链接。我已经尝试了各种替换函数,并在JS中,但我要么做错了,或它不起作用。



任何帮助将不胜感激。

解决方案

您可以获取具有您正在查找的类名的li的子元素,然后更改innerHTML



例如:

  var elements = document.getElementsByClassName(time)[0] .getElementsByTagName(a); $ var 
$ b for(var i = 0,j = elements.length; i elements [i] .innerHTML =Time Review;
}

当然,这里假定有一个名为time的元素页。您还需要注意检查是否有空值。


I'm trying to change the name of a link, however, I have some restrictions. The link is placed in code that looks like this:

<li class='time'> 
<a href="#time">Review Time</a> 
<img alt="Styled" src="blah" /> 
</li> 

Basically, I have a class name to work with. I'm not allowed to edit anything in these lines, and I only have a header/footer to write Javascript / CSS in. I'm trying to get Review Time to show up as Time Review, for example.

I know that I can hide it by using .time{ display: hide} in CSS, but I can't figure out a way to replace the text. The text is also a link, as shown. I've tried a variety of replace functions and such in JS, but I'm either doing it wrong, or it doesn't work.

Any help would be appreciated.

解决方案

You could get the child elements of the li that has the class name you are looking for, and then change the innerHTML of the anchor tags that you find.

For example:

    var elements = document.getElementsByClassName("time")[0].getElementsByTagName("a");

    for(var i = 0, j = elements.length; i<j; i++){
        elements[i].innerHTML = "Time Review";
    }

Of course, this assumes that there is one element named "time" on the page. You would also need to be careful about checking for nulls.

这篇关于Javascript - 链接名称更改与限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆