6

What is the way for having a graphical component (more precisely a twitter.bootstrap icon) in an html website calling a java script.

One could either make a button and putting the icon on it, but this does not look nice IMHO. Or one could use the href tag,

<a href="#" name="ad_fav" onclick= CALLFUNCTION> <i
                        class="icon"></i></a>

But what is the cleanest way of achieving this? It would also be nice if the icon could change after it was clicked.

How it for example the upvote button in stackoverflow implemented?

4

4 回答 4

4

另一种方式:

function myFunc () {
// code here
}

var element = document.getElementsByClassName("icon");
element[0].addEventListener("click", myFunc);
于 2013-06-24T12:03:48.397 回答
1

只需将 <a> 的 href 设为 'javascript:',例如:

<a href="javascript:alert('hello there! this works!')" name="ad_fav"> <i
                    class="icon"></i></a>

如果需要,将 alert(...) 替换为您的函数调用

于 2013-06-24T12:02:24.437 回答
1

您不必用锚元素包装它:

<img src="[path to twitter.bootstrap icon]" onclick="yourJavaScriptFunction" />
于 2013-06-24T12:03:02.687 回答
0

为什么不在函数上使用 jquery?

$(document).on("click", "a.icon", function (e) {
    e.preventDefault();
});
于 2013-06-24T14:35:18.427 回答