プロが教える店舗&オフィスのセキュリティ対策術

自動目次作成のjavascriptの改変についてよろしくお願いします。

①以下のスクリプトを
<h2 class="midashi2"></h2>
<h3 class="midashi3"></h3>

このh2のmidashi2とh3のmidashi3だけを抽出するように改変できないでしょうか?

もしそれができないのならば

②記事中で使われている見出しのトップレベルがh2なので目次に含める見出しタグをh2とh3だけに変更したいです。

よろしくお願い致します。

<!--以下はわかる人のみ編集してください-->
<script type="text/javascript">
<!--
//ドキュメントのロード完了で処理開始
$(document).ready(function() {

//本処理
$.fn.blogeasyindex=function(){

var ulid = "indexUL" //ulのid名

//ul作成
$("<ul></ul>")
.attr("id",ulid)
.prependTo(this);

//h要素の検索
$(":header").each(function(n){

var level = "indexH1";
if(this.nodeName.toLowerCase() == "h1") {
level = "indexH1"; //h1のliのclass名
} else if(this.nodeName.toLowerCase() == "h2") {
level = "indexH2"; //h2のliのclass名
} else if(this.nodeName.toLowerCase() == "h3") {
level = "indexH3"; //h3のliのclass名
} else if(this.nodeName.toLowerCase() == "h4") {
level = "indexH4"; //h4のliのclass名
} else if(this.nodeName.toLowerCase() == "h5") {
level = "indexH5"; //h5のliのclass名
} else if(this.nodeName.toLowerCase() == "h6") {
level = "indexH6"; //h6のliのclass名
}

//目次作成
$("<li></li>")
.attr("class",level)
.append("<a href='#_"+n+"'>"+ $(this).text()+"</a>")
.appendTo("#"+ulid);

//アンカー作成
$("<a></a>")
.attr("id","_"+n)
.insertBefore(this);

});

return $(this) ;

}

$("#indexDV").blogeasyindex(); //indexDVに対して目次出力開始

});
//-->
</script>

A 回答 (2件)

$(document).ready(function(_) {


_ ; $.fn.blogeasyindex = function(){
_ ; _ ; var that = $(this);
_ ; _ ; var ul = $("<ul id='indexUL'></ul>").prependTo(that);
_ ; _ ; var targets = ["h2.midashi2", "h3.midashi3"].join(',');
_ ; _ ; $(targets).each(function(n,e){
_ ; _ ; _ ; var id = '_' + n;
_ ; _ ; _ ; var level = 'index' + e.nodeName;
_ ; _ ; _ ; $("<a></a>", {id:id}).insertBefore(e);
_ ; _ ; _ ; var a = $('<a></a>', {href:'#'+id}).text($(e).text());
_ ; _ ; _ ; $("<li></li>", {'class':level}).append(a).appendTo(ul);
_ ; _ ; });
_ ; _ ; return that;
_ ; };
_ ; $("#indexDV").blogeasyindex();
});
    • good
    • 1

<script>


//ドキュメントのロード完了で処理開始
$(document).ready(function () {

//本処理
$.fn.blogeasyindex = function () {

var ulid = "indexUL" //ulのid名

//ul作成
$("<ul></ul>")
.attr("id", ulid)
.prependTo(this);

//h要素の検索
$("h2, h3").each(function (n) {

var level = "indexH1";
if (this.nodeName.toLowerCase() == "h1") {
level = "indexH1"; //h1のliのclass名
} else if (this.nodeName.toLowerCase() == "h2") {
level = "indexH2"; //h2のliのclass名
} else if (this.nodeName.toLowerCase() == "h3") {
level = "indexH3"; //h3のliのclass名
} else if (this.nodeName.toLowerCase() == "h4") {
level = "indexH4"; //h4のliのclass名
} else if (this.nodeName.toLowerCase() == "h5") {
level = "indexH5"; //h5のliのclass名
} else if (this.nodeName.toLowerCase() == "h6") {
level = "indexH6"; //h6のliのclass名
}

//目次作成
$("<li></li>")
.attr("class", level)
.append("<a href='#_" + n + "'>" + $(this).text() + "</a>")
.appendTo("#" + ulid);

//アンカー作成
$("<a></a>")
.attr("id", "_" + n)
.insertBefore(this);

});

return $(this);

}

$("#indexDV").blogeasyindex(); //indexDVに対して目次出力開始

});
</script>
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!