jquery将ID添加到所有段,使它们都可以链接。 [英] jquery to add ids to all paras to make them all linkable.

查看:78
本文介绍了jquery将ID添加到所有段,使它们都可以链接。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有数千个htm文件的网站。有时我需要派人到特定的段落阅读。
我在寻找的是一个jQuery插件,它简单地为所有段落添加id,使它们可以链接,所以我们可以将它们发送到: http://www.demo.com/index.html#p_10

解决方案

为什么选择插件?并且不需要ID



如果将脚本插入到页面或现有的外部脚本中,这应该可行



注意:这不会以任何方式修改DOM,并且会比.each更快

  var pIndexPos = location.hash.indexOf(p_); 
if(pIndexPos!= - 1){
var pIndex = parseInt(location .hash.substring(pIndexPos + 2),10)+1; // 1based for nnth
var offsetTop = $(p:nth-​​child(+ pIndex +))。offset()。top- 20; // - 项目的高度
$('html,body')。animate({
scrollTop:offsetTop
},500);
}
} );


I have a site with thousands of htm documents on them. Sometimes I need to send someone to a specific paragraph to read. What I am looking for is a jquery plugin which simply adds ids to all the paragraphs, making them linkable, so we could send them to: http://www.demo.com/index.html#p_10 for instance.

解决方案

Why a plugin? And no need for an ID

This should work if you insert a script into the page or into an existing external script

NOTE: This does NOT modify the DOM in any way and is WAY faster than an .each

$(function() {
  var pIndexPos = location.hash.indexOf("p_");
  if (pIndexPos!=-1) {
    var pIndex = parseInt(location.hash.substring(pIndexPos+2),10)+1; // 1based for nth
    var offsetTop = $("p:nth-child("+pIndex+")").offset().top-20; // - height of item
    $('html, body').animate({
      scrollTop: offsetTop
    },500);
  }
}); 

这篇关于jquery将ID添加到所有段,使它们都可以链接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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