【问题标题】:Change HTML element and restore更改 HTML 元素并恢复
【发布时间】:2014-02-18 23:41:32
【问题描述】:

我有一个包含 2 个链接的网站。

一旦用户点击一个链接,它就必须变成一个没有链接的 h2 标题。
当用户点击第二个链接时,第一个链接必须恢复到之前的状态,第二个链接应该成为一个 h2 元素。

这些链接的目的是显示一个新标签,因此它们不会重新加载页面。

HTML(不要介意 href 链接)

<div id="Panel" class="header-panels">
    <a data-linked="#panelImages" href=".../images/#" tabindex="0" class="active">Foto's</a>
    <a data-linked="#panelVideos" href=".../videos/#" tabindex="0">Video's</a>
</div>

有没有办法用 javascript/jquery 做到这一点?

我尝试过类似的东西:

var p = $('.header-panels .active');
var a = $('<h2></h2>').append(p.contents());
p.replaceWith(a);

它可以将a标签更改为h2,但是当用户第二次点击时,我似乎无法重新创建具有所有属性的a标签。

或者有人知道更好的方法吗?

提前谢谢?

【问题讨论】:

  • 如果不是,是否需要将其设为&lt;h2&gt; 标记 jQuery 有两个函数,addClass() 和 removeClass(),效果很好。
  • 如何在元素上使用显示/隐藏功能,以便切换链接隐藏 并显示

    ,反之亦然?

  • 感谢 cmets,但是 H2 是必须的。显示/隐藏的想法听起来像是一种有趣的方法。我会看看这个!

标签: javascript jquery html


【解决方案1】:

Fiddle Demo

var all_a = $('#Panel a'); //cache your selector
var h2 = $('<h2/>').attr('id', 'temp');//created a h2 tag with id temp (just for identification if u need to style it)
all_a.click(function (e) {
    e.preventDefault();//stop default behavior of click if u want
    all_a.show(); //show all a tag inside id Panel
    var $this = $(this); //cache selectore
    h2.text($this.text()).insertBefore($this); //insert h2 before current a tag clicked  and with it's text
    $this.hide(); //hide clicked a
});

.insertBefore()

【讨论】:

  • 这对我帮助很大!谢谢!
【解决方案2】:

这个脚本让你在不添加第三个元素的情况下切换'h2'和'a',它只是用'a'元素替换'h2',并将'a'元素的所有参数添加到'h2'元素上数据的形式。

$(document).on('click', '#Panel a', function (e) {
    e.preventDefault();
    $this = $(this);

    var $h2Elem = $this.parents('#Panel').find('h2');
    console.log($h2Elem);
    $h2Elem.each(function (index, item) {
        var linked = $(item).data('linked'),
            href = $(item).data('href'),
            tabindex = $(item).data('tabindex'),
            text = $(item).text();
        $(item).replaceWith('<a data-liked="' + linked + '" href="' + href + '" tabindex="' + tabindex + '">' + text + '<a>');
    });

    var linked = $(this).data('linked'),
        href = $(this).attr('href'),
        tabindex = $(this).attr('tabindex'),
        text = $(this).text();
    $this.replaceWith('<h2 data-liked="' + linked + '" data-href="' + href + '" data-tabindex="' + tabindex + '">' + text + '<h2>');
});

小提琴HERE

【讨论】:

  • 感谢您的帮助。这段代码似乎有效,但是当您检查元素时,它会在您单击几次时生成很多无用的 html。所以它并不是最优的。
【解决方案3】:

为什么不使用单独的元素并根据需要隐藏/显示它们?例如

<div id="Panel" class="header-panels">
  <a data-linked="#panelImages" href=".../images/#" tabindex="0" class="active"     id="fotolink">Foto's</a><h2 id="foto" style="display: none;">Foto's</h2>
  <a data-linked="#panelVideos" href=".../videos/#" tabindex="0"     id="videolink">Video's</a><h2 id="video" style="display: none;">Video's</h2>
</div>

<script type="text/javascript">
  $("#fotolink").on("click", function (e) {
    $("#fotolink").hide();
    $("#foto").show();
    $("#videolink").show();
    $("#video").hide();
    //e.preventDefault();
  });
  $("#videolink").on("click", function (e) {
    $("#fotolink").show();
    $("#foto").hide();
    $("#videolink").hide();
    $("#video").show();
    //e.preventDefault();
  });
</script>

编辑:

根据您的评论,我将存储对旧锚元素的引用并使用detach()。例如

  var oldLink;
  function removeH2() {
    var h2 = $("#textheading");
    if(h2) {
      $(oldLink).insertBefore(h2);
      $(h2).remove();
    }
  }
  function toText(a) {
    oldLink = a;
    var h2 = $('<h2 id="textheading">' + $(a).text() + '</h2>');
    $(h2).insertAfter(a);
    $(a).detach();
  }
  $("#fotolink").on("click", function (e) {
      //e.preventDefault();
      removeH2();
      toText(e.target);
  });
  $("#videolink").on("click", function (e) {
      //e.preventDefault();
      removeH2();
      toText(e.target);
  });

【讨论】:

  • 分离元素是一个选项,但是 HTML 是在 C# 中动态生成的。这就是为什么我想要一个解决方案来使用 javascript/jquery 更改它而不触及 HTML。但它是一个很好的备份解决方案。
猜你喜欢
  • 2012-07-09
  • 2016-06-17
  • 2022-09-23
  • 2018-06-23
  • 1970-01-01
  • 2019-03-28
  • 2017-01-06
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多