简体   繁体   English

如何替换HTML标签?

[英]How to replace a Html Tag?

Please check my code: 请检查我的代码:

<div id="seconds">
  <a onclick="ChangeRefreshSeconds(15,this);" href="javascript:;">15</a>
  <b><span style="background-color:yellow;">30</span></b>
  <a onclick="ChangeRefreshSeconds(60,this);" href="javascript:;">60</a>
  <a onclick="ChangeRefreshSeconds(120,this);" href="javascript:;">120</a>
</div>

My current number is 30 When i click on the number 60 it should be- 我当前的电话号码是30当我单击电话号码60它应该是-

<div id="seconds">
  <a onclick="ChangeRefreshSeconds(15,this);" href="javascript:;">15</a>
  <a onclick="ChangeRefreshSeconds(30,this);" href="javascript:;">30</a>
  <b><span style="background-color:yellow;">60</span></b>
  <a onclick="ChangeRefreshSeconds(120,this);" href="javascript:;">120</a>
</div>

Javascript: 使用Javascript:

<script>
function ChangeRefreshSeconds(s,t){
    //t.innerHTML='<span style="background-color:yellow;">'+s+'</span>';
    var textnode=document.createTextNode('<span style="background-color:yellow;">'+s+'</span>');
    t.replaceChild(textnode,t);
}
</script>

I am sure that i have written my JavaScript code in the wrong way. 我确定我以错误的方式编写了我的JavaScript代码。

Please solve my above code. 请解决我上面的代码。

You can replace tags for sure. 您可以确定替换标签。 But you don't have to do that. 但是您不必这样做。 It's much simple and less obtrusive to go with just CSS approach. 仅使用CSS方法就很简单,也不太麻烦。 With CSS when you have class active you have great deal of flexibility to style currently selected item whatever you want. 使用CSS时,你有一流的active ,你有大量的灵活性样式任何你想要的当前选择的项目。

 function ChangeRefreshSeconds(seconds, obj) { // do nothing if it's already active if (obj.className === 'active') return; var a = document.getElementById('seconds').children; for (var i = 0; i < a.length; i++) { a[i].className = ''; } obj.className = 'active'; // do something else alert('Current seconds: ' + seconds); } 
 #seconds .active, #seconds .active:active { background: yellow; cursor: default; color: #333; font-weight: bold; } 
 <div id="seconds"> <a onclick="ChangeRefreshSeconds(15, this);" href="javascript:;">15</a> <a onclick="ChangeRefreshSeconds(30, this);" href="javascript:;" class="active">30</a> <a onclick="ChangeRefreshSeconds(60, this);" href="javascript:;">60</a> <a onclick="ChangeRefreshSeconds(120, this);" href="javascript:;">120</a> </div> 

Something like this would be a lot easier with jQuery , if you have the possibility to use it, i would definitely advise you to. 使用jQuery这样的事情会容易得多 ,如果您可以使用它,我绝对会建议您使用。

As far as i can see your code only changes to <span> and doesn't change the already changed <span's> back to <a> . 据我所知,您的代码仅更改为<span> ,而不会将已经更改的<span's>更改回<a> I assumed that's what you wanted? 我以为那就是你想要的? The piece of code for this script (in jQuery) would look something like: 该脚本的代码段(在jQuery中)如下所示:

$("div#seconds a").click(function(){
    var originalValue = $(this).html();
    var spanValue = $("b span", $(this).parent()).html();

    $("b", $(this).parent()).replaceWith('<a href="javascript:;">'+spanValue+'</a>');
    $(this).replaceWith('<b><span style="background-color:yellow;">'+originalValue+'</span></b>');
});

try this: 尝试这个:

<script>
    function ChangeRefreshSeconds(s,t){
        var textnode=document.createTextNode('<span style="backgroundcolor:yellow;">'+s+'</span>');
        t.parentNode.replaceChild(textnode,t);
     }
 </script>

Following is my approach, try out this 以下是我的方法,请尝试一下

 function ChangeRefreshSeconds(event){ event = event || window.event; var seconds = document.getElementById('seconds'); var anchors = seconds.getElementsByTagName('a'); var i; for (i = 0; i < anchors.length; i++) { anchors[i].className = ''; } event.target.className = 'active'; } 
 .active { background-color:yellow; } 
 <div id="seconds"> <a onclick="ChangeRefreshSeconds(event);" href="javascript:;">15</a> <a onclick="ChangeRefreshSeconds(event);" href="javascript:;">30</a> <a onclick="ChangeRefreshSeconds(event);" href="javascript:;">60</a> <a onclick="ChangeRefreshSeconds(event);" href="javascript:;">120</a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM