【问题标题】:Get character number position of tag in dom from a specific point从特定点获取 dom 中标签的字符编号位置
【发布时间】:2017-12-22 04:42:42
【问题描述】:

我有一个 div,其中包含未知数量的 html 标签或它们的深度。例如,div 内的 HTML 可以是任意数量的标签和标签的深度。

<div id="content">
    <p> Hello this is
        <ul>
            <li>**{I CLICK HERE}**A test<p> and some more</p></li>
        </ul>
        Lorem ipsum
    </p>
</div>

如果我单击 {I CLICK HERE},我想获得的是从#content div 到所单击内容的父标记开头的字符编号位置。所以在这个例子中,我想要“li”标签的#content div中的字符数。

创建书签系统的最终目标是在数据库中保存位置后我可以返回到该位置,并且当将来重新加载此内容时,我可以包装该标签或在其上放置一个类以便能够滚动到该位置。

到目前为止,我可以抓取节点,但不确定如何获得它的位置。

$("#content").click(function(event) {
    console.log(event.target.nodeName);
});

我的想法是,一旦我获得了职位,我就将其保存到数据库中。当页面重新加载时,我将一个锚跨度标签插入该位置,然后使用它来滚动到。我将使用服务器端代码在稍后的页面加载中放置锚点。

或者,如果有更好的方法来实现能够使用 javascript 在重新加载页面时返回(scrollTo)点击点的最终结果,请告诉我。数据将保存在数据库中,并在新的页面加载时拉回。

【问题讨论】:

  • 您希望本示例中的“字符数”是多少?
  • 只是为了(尝试)并且绝对清楚,您要查找的字符数是由innerHTML 字符串的this image 的选择高亮表示的字符还是其他?如果我似乎在吹毛求疵,我很抱歉,但我仍然不太确定你指的是什么,这使得我很难回答这个问题。
  • 我猜你会动态加载 &lt;li&gt; 元素,为什么不为每个项目设置唯一的 id 属性?
  • @DavidThomas 是的,这在图片中是正确的。
  • @Gonzalo #content 中的文本例如是教育教科书的一整章。我试图让用户在文本中添加书签,无论文本是什么。内容不是动态创建的,而是从已经转换为 html 的章节的文本数据库中提取的。

标签: javascript jquery


【解决方案1】:

解决此问题的一种方法如下:

function innerHTMLCharCountFrom(event) {

  // here we retrieve the node upon which the event,
  // for which the function was listening, was fired,
  // and retrieve its outerHTML (the HTML string of its
  // contents and both opening, and closing, tags):
  let needle = event.target.outerHTML,

    // 'this' is the node upon which the event-listener
    // was bound, and passed from EventTarget.addEventListener();
    // here we retrieve its innerHTML:
    haystack = this.innerHTML,

    // we use String.prototype.substring() to get a section
    // of the innerHTML String from the start index of 0 to
    // the index returned by String.prototype.indexOf(),
    // which is the starting position of the event.target's
    // opening tag (specifically the index of the '<' character
    // in that opening tag):
    temp = haystack.substring(0, haystack.indexOf(needle)),

    // here we create an Object to contain both the found String,
    // the innerHTML of the 'this' node until the beginning '<'
    // of the event.target node ('innerHTMLString') and the length
    // of that String of HTML ('length'):
    result = {
      'innerHTMLString': temp,
      'length': temp.length
    }

  // logging the result Object to the console:
  console.log(result);

  // returning the result Object to the calling context:
  return result;
}

// using document.querySelector() to use a CSS selector to identify
// the element upon which we wish to bind the event-listener:
document.querySelector('#content')

  // binding the 'innerHTMLCharCountFrom()' function (note the
  // deliberate lack of parentheses) as the event-handler for
  // the 'click' event:
  .addEventListener('click', innerHTMLCharCountFrom);

function innerHTMLCharCountFrom(event) {

  let needle = event.target.outerHTML,
    haystack = this.innerHTML,
    temp = haystack.substring(0, haystack.indexOf(needle)),
    result = {
      'innerHTMLString': temp,
      'length': temp.length
    }

  console.log(result);
  return result;
}
document.querySelector('#content').addEventListener('click', innerHTMLCharCountFrom);
<div id="content">
  <p> Hello this is</p>
  <ul>
    <li>**{I CLICK HERE}**A test
      <p> and some more</p>
    </li>
  </ul>
  <p>Lorem ipsum</p>
</div>

JS Fiddle demo.

【讨论】:

  • 谢谢。当我放入简短的演示内容并将其插入我的 php 中时它可以工作,该 php 插入一个锚标记以滚动到但当我放入真正的章节内容时停止工作。我相信存在某种问题计数字符和 php 之间的差异和javascript。我不知道当我尝试放入制表符、新行、额外空格不同的 dom 结构来测试它时,一切似乎都工作正常,所以我不确定是什么导致了服务器端和客户端之间的计数问题差异。停止工作 = 服务器和客户端之间的字符数不同。
  • 您可能面临的一个问题是,在 PHP 脚本中,由于代码缩进以及可能使用 \t 到(以及其他字符)到整理 HTML 文档中的演示文稿。我不确定该建议什么作为替代方案,但我会尝试考虑替代方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2012-06-14
  • 2013-09-04
相关资源
最近更新 更多