【问题标题】:Changing the letters of a word reformat the whole html更改单词的字母重新格式化整个 html
【发布时间】:2020-10-19 13:50:18
【问题描述】:

我有这个script,当你悬停它们时,它会改变内容的所有字母。

实际上它改变了页面的整个格式并粘合了所有内容。

我被告知主要问题在于这部分:

var letters = $('p').text();

就那样做

$("p").each(function() {/*$(this) is the current paragraph here*/});

可以解决重复和格式化问题

但我不知道如何使用它,因为我对这一切都很陌生。 非常感谢您的帮助。

function nextLetter(ch) {
    if (!ch.match(/[a-z]/i)) return ch;
    else if (ch === 'Z') return 'A';
    else if (ch === 'z') return 'a';
    return String.fromCharCode(ch.charCodeAt(0) + 1);
}
$(document).ready(function(){
  var letters = $('p').text();
  var nHTML = '';
  for(var letter of letters) {
    nHTML+="<span class='x'>"+letter+"</span>";
  }
  $('p').html(nHTML);
  $(".x").hover(function(e) {
    if (e.type === "mouseenter") $(this).text(nextLetter($(this).text()));
  });
})
.wrapper {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-gap: 10px;
  grid-auto-rows: minmax(100px, auto);

}
.one{
   grid-column: 1 /5;
  grid-row: 1;
    background-color:pink;
}
.two{
   grid-column: 5 / 8;
  grid-row: 1;
  background-color:yellow;
}
.three{
   grid-column: 8 / 13;
  grid-row: 1;
  background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="one"><p>I'm the text</p></div>
  <div class="two"><p><a>I'm the link in the page</a>
    <a href="http://vimeo.com/" target="_blank" style="color: rgb(82, 19, 197);">vimeo</a><sup>➶</sup>
  </p></div>


</div>

【问题讨论】:

  • 你没有问问题。这段代码应该做什么?相反,它做了什么?为什么这是错的?谁告诉你你需要改变那一点,他们到底说了什么?
  • 是的,抱歉,此时脚本应该将字母更改为字母表上的下一个字母。但此刻,我的这个脚本正在复制我的文本,如您所见,并重新格式化链接和所有格式。我只想让脚本更改字母而不复制和更改链接格式或断开链接。

标签: javascript letter


【解决方案1】:

基本上,您需要处理第一段的内部文本和第二段锚点的内部文本,因此您需要相应地更改您使用的选择器。 .text() 粘合所有匹配标签的内部文本,这是粘合文本的原因。这意味着 .text() 需要在已更改选择器循环的 function 回调内的 $(this) 上调用。我只需要对你的 JS 代码做一些细微的改动,但细微的改动很重要:

function nextLetter(ch) {
    if (!ch.match(/[a-z]/i)) return ch;
    else if (ch === 'Z') return 'A';
    else if (ch === 'z') return 'a';
    return String.fromCharCode(ch.charCodeAt(0) + 1);
}
$(document).ready(function(){
  $('.one p, .two p a').each(function() {
      var letters = $(this).text();
      var nHTML = '';
      for(var letter of letters) {
          nHTML+="<span class='x'>"+letter+"</span>";
      }
      $(this).html(nHTML);
      $(".x").hover(function(e) {
          if (e.type === "mouseenter") $(this).text(nextLetter($(this).text()));
      });
})});

小提琴:https://jsfiddle.net/4e20tpku/

在这里,我假设您也希望能够更改第二段第二个链接中的 vimeo 字母。如果这个假设不正确,则需要对我一直使用的选择器进行轻微更改。

【讨论】:

  • 感谢您的帮助。完美运行。
【解决方案2】:

你可能想要这样的东西:

$(document).ready(function() {
  var paragraphs = $('p');     // select all paragraphs
  paragraphs.each(function() { // loop over them
    var letters = $(this).text(); // $(this) accesses the current loop item
    var nHTML = '';
    for (var letter of letters) {
      nHTML += "<span class='x'>" + letter + "</span>";
    }
    $(this).html(nHTML);
  });
  $(".x").hover(function(e) {
    if (e.type === "mouseenter") $(this).text(nextLetter($(this).text()));
  });
})

【讨论】:

  • 非常感谢,它似乎停止了文本重复,但它破坏了链接。
  • 是的,当然可以——因为您阅读段落的内容只是作为开始的文本,这意味着链接已经“消失”了。如果你想要一个合适的解决方案,那么你需要实际遍历 DOM,并沿着每个“分支”向下直到你到达一个实际的文本节点,然后只专门替换它。
  • 现有的库可以在更复杂的级别上实现这一点,例如github.com/davatron5000/Lettering.js
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多