jquery tweets
by techbubble
HTML
<script src="http://github.com/kerberoS/jQuery-Tweets/raw/master/js/jquery.tweets.0.1.js"></script>
<div id="main">
<h1>Click on any item</h1>
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<button class="test"></button>
<button></button>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
<h2>Log</h2>
<div id="log"></div>
CSS
div, button, li { cursor: pointer; }
h1 { margin: 10px; font-size: 14pt; font-weight: bold; }
h2 { margin: 10px; font-size: 12pt; font-weight: bold; }
#main div, #main button, #main li { padding: 5px; border: dotted 1px #cc0000; }
JavaScript
$("#main div, #main li, #main button").click(function(event) {
var hash = getElementHash($(this), event.target.nodeName);
$("#log").append((hash !== "" ? hash : event.target.nodeName +
" not hashable") +
"<br />");
$("#log").append("Test of " + event.target.nodeName +
" would return " +
(getElementByHash(hash) === null ? " No Match" : "Match") +
"<br />");
return false;
});
function getElementHash(el, tag)
{
var hashKey = "H";
var hashContent = $.trim(el.html());
if (hashContent === "")
{
hashContent = getElementAttributes(el);
if (hashContent !== "")
hashKey = "A";
else
hashKey = "";
}
if (hashKey !== "")
return(tag + ":" + hashKey + ":" + getHash(hashContent));
else
return "";
}
function getElementAttributes(el)
{
var allAttribs = el[0].attributes;
attribs = "";
for(var i=0;i<allAttribs.length;i++) {
attribs += allAttribs[i].nodeName + "-" + allAttribs[i].nodeValue + ";";
}
return attribs;
}
function getElementByHash(hashValue)
{
var el = null;
var frags = hashValue.split(":");
if (frags.length !== 3)
return null;
else
{
var tag = frags[0];
var hashKey = frags[1];
var hash = parseInt(frags[2]);
$("#main " + tag).each(function() {
if (hashKey == "H")
{
if ((getHash($(this).html()) - hash) == 0)
{
el = $(this);
return false;
}
}
else
{
if ((getHash(getElementAttributes($(this))) - hash) == 0)
{
el = $(this);
return false;
}
}
});
}
...