我希望添加点击特定HTML文本的选项,并执行正确的JavaScript代码的功能。
我该怎么做?
为了语义化,我会使用一个像这样的<button>
元素:
<button class="link">Clicky</button>
要使按钮看起来像普通文本,您可以使用CSS:
button.link { background:none; border:none; }
为了方便处理点击事件,我会使用jQuery,代码如下:
$(".link").on("click", function(e) {
// e is the event object
// this is the button element
// do stuff with them
});
如果你的按钮有ID属性,你可以使用纯JS代码来实现:
var button = document.getElementById("your-button-id");
button.addEventListener("click", function(e) {
// e is the event object
// e.target is the button element
// do stuff with them
});
<div id="text" onClick="function()"> your text here </div>
<button>Label</button>
或者(<input type="button" value="Label">
)。 (您可以使用CSS样式屏蔽边框和背景)。click
事件处理程序绑定到它上面。<span class="yourClass">text</span>
的span或其他元素中,然后使用JavaScript添加事件侦听器(最好在HTML末尾)。为了简洁起见,我将使用jQuery进行演示,但也可以使用本机JavaScript。<script>
$('.yourClass').on('click',function(){
//your javacript
});
</script>
或者你可以通过搜索整个页面来查找该单词的实例,但我不建议这样做,因为它会使事情变得非常缓慢。
background: transparent;
,none
不是一个有效的颜色值。 - run_the_racebackground: transparent
等同于background: none
,也等同于background: repeat scroll 0% 0% / auto padding-box border-box none transparent;
...background
不仅仅是background-color
的简写,详见:https://developer.mozilla.org/en-US/docs/Web/CSS/background#values - JKirchartz