自動目次作成の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件)
- 最新から表示
- 回答順に表示
No.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();
});
No.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>
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
追加ボタンを押した際に ok ボ...
-
GASでチェックボックスを一括of...
-
特定の文字列を複数抜き出した...
-
jsonテキストデータの並び替え...
-
jQueryで同じクラス名のものを...
-
鍵盤アプリで、スマホの画面に...
-
jQueryでシンセサイザーを作っ...
-
HTMLでサブフレームから親のス...
-
ボタンを押したあとに画像を表...
-
disabled プロパティが表示され...
-
初心者です。gulpでコンパイル...
-
食材の期限を管理するためにGAS...
-
ビデオのJSについて
-
プログラミング 学習
-
ジャバスクリプトについて。
-
Colorboxがうまく設置できません
-
jsで質問です。 ボタンが二つ存...
-
前回の質問の続き function mov...
-
このプログラムに、王手をかけ...
-
前回の質問の続き function mou...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
bxsliderで画像毎に表示時間を...
-
jQueryでメニューの開閉スライド
-
複数の自動スライドショー(jQue...
-
bxsliderで巻き戻しにする方法
-
if文でelseが実行されない
-
要素ごとにイベントが発生させたい
-
タブをフリックでスライドでき...
-
Javascriptを使ってQRコード読...
-
JavaScriptでtabindexの変更っ...
-
二つのbxsliderをレスポンシブ...
-
readyStateが4にならない原因
-
階層別の組織図の自動作成について
-
[jQuery UI] sortableを使いaja...
-
JavascriptからPHPへのAjax通信...
-
AjaxでJSONを受信すると、文字...
-
jQueryのblockUIをformのボタン...
-
.txtファイルの読み込み
-
SQLのmaxで求めた値を変数に代...
-
要素内を常に一番下を表示させたい
-
カンマ区切りのデータを配列に...
おすすめ情報