Created
April 25, 2012 21:27
-
-
Save xeoncross/2493590 to your computer and use it in GitHub Desktop.
Convert DOM element words into click-able list of spans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(function(){ | |
var text = $("#select_words").text().trim().split(" ").join("</span> <span>"); | |
$("#select_words").html("<span>" + text + "</span>"); | |
$("span").click(function () { | |
var $p = $(this).parent('div'); | |
console.log($(this).prev().html()); | |
console.log($(this).html()); | |
console.log($(this).next().html()); | |
$(this).remove(); | |
console.log($p.text()); | |
}); | |
$('span').hover(function() | |
{ | |
console.log($(this).html()); | |
}); | |
}); | |
</script> | |
<style type="text/css"> | |
span { background: #ccc; } | |
</style> | |
</head> | |
<body> | |
<div> | |
This is normal text. | |
</div> | |
<div id="select_words"> | |
This text is selectable and click-able. | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment