web-dev-qa-db-ja.com

すべてのhtmlリンクを作成して同じタイトルを共有することは可能ですか?

問題は、各行に同じタイトルのリンクが含まれる〜2000行を含むテーブルがあることです。

<a id="1" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere1">My 1st link</a>
[...]    
<a id="2000" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere2000">My 2000th link</a>

この非常に長いタイトル文字列を2000回も持たないことが可能かどうか疑問に思っています!?

2
hoang

1つの解決策は、ページがロードされたらJavascriptを使用してすべてのリンクタイトルを追加することです。これは、ユーザーのダウンロードが小さいことを意味します-文字列の実際の長さによっては、より多くのメモリを消費する可能性があります。別の考えられるマイナス面:検索エンジンと一部のスクリーンリーダーは、リンクのタイトルを表示しない場合があります。

最初にクラスをリンクに追加します。肥大化を可能な限り低く抑えるために非常に短くしましたが、よりわかりやすいクラス名が必要な場合があります。

<a id="1" class="t" href="goThere1">My 1st link</a>
[...]    
<a id="2000" class="t" href="goThere2000">My 2000th link</a>

次に、このJavascriptを追加します(テストされていませんが機能するはずです):

<script type="text/javascript">
var titleString = "A very long string common to all my links that i would like to have only once in the file in order to gain weight."
var links = document.getElementsByClassName('t');
for ( var l in link )
{
    l.setAttribute('title', titleString);
}
</script>
3
DisgruntledGoat

各リンクにタイトルを付けずに、各<A>要素でtitle属性を有効にしたい場合は、DIVにタイトル属性をネストし、title属性をDIVに割り当てます。

たとえば、比較します http://jsfiddle.net/tgew9/

<a id="1" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere1">My 1st link</a>
[...]    
<a id="2000" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere2000">My 2000th link</a>?

http://jsfiddle.net/tgew9/1/

<div title="A very long string common to all my links that i would like to have only once in the file in order to gain weight">
<a id="1" href="goThere1">My 1st link</a>
[...]    
<a id="2000" href="goThere2000">My 2000th link</a>
</div>
2
Metalshark

リンクがデータベース内にある場合、いくつかのオプションがあり、どちらも簡単です。

1)データベースを更新して、タイトルタグを含めます。タイトルタグ用の列がまだない場合は、作成してからこのクエリを実行します。UPDATE tablename SET title = '体重が増加する';。次に、それらのリンクを使用するページを動的に生成するたびに、残りのリンクとともにタイトルを引き出して印刷します。

2)テキストはすべてのリンクで同じであるため、コードに直接ハードコーディングしてください。 PHPの例を次に示します。

while ($row = mysql_fetch_assoc($result))
{

    echo '<a href="' . $row['url'] . '" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight">' . $row['anchor_text'] . '</a>';

}
0
John Conde