添加工具提示,其值与范围值相同

Too*_*ook 0 html javascript jquery

如果我有<span>some text</span>,我怎么能这样:

<span title="*whatever is between span tags:* some text (in this case)">some text</span>

Mar*_*c B 5

如果您自己构建HTML,那么只需在生成html时将文本添加到title属性中.例如,在PHP中它将是:

$text = 'some text';

<span title="<?php echo htmlspecialchars($text) ?>"><?php echo $text ?></span>
Run Code Online (Sandbox Code Playgroud)

如果你在事后通过Javascript做,那么(使用jquery):

$('span').each(function () {
     this.attr('title', this.text());
});
Run Code Online (Sandbox Code Playgroud)