เมื่อคุณจะคลิกที่องค์ประกอบที่มีแอตทริบิวต์ชื่อ องค์ประกอบลูกที่มีข้อความชื่อจะถูกผนวกเข้าไป เรามาดูตัวอย่างกัน −
สำหรับ HTML -
<p> The <span class="demo" title="this is underscore">underlined</span> character. </p>
jQuery -
$("span[title]").click(function () {
var $title = $(this).find(".title");
if (!$title.length) {
$(this).append('<span class="title">' + $(this).attr("title") + '</span>');
} else {
$title.remove();
}
}); ต่อไปนี้คือ CSS −
.demo {
border-bottom: 2px dotted;
position: relative;
}
.demo .title {
position: absolute;
top: 15px;
background: gray;
padding: 5px;
left: 0;
white-space: nowrap;
}