假设我希望每次用户单击任何链接时都会弹出一个警告,显示“hohoho”。
我是否需要在每个链接中添加
onclick = "alert('hohoho')"
,还是可以使用CSS设置,以便与每个链接一起使用?onclick = "alert('hohoho')"
,还是可以使用CSS设置,以便与每个链接一起使用?仅通过CSS是无法实现的,但是您可以使用JavaScript来实现,并且(可选)jQuery。
如果您想不使用jQuery来实现:
<script>
window.onload = function() {
var anchors = document.getElementsByTagName('a');
for(var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function() {
alert('ho ho ho');
}
}
}
</script>
如果要在不使用jQuery的情况下,只针对特定类别(例如:hohoho
)进行操作:
<script>
window.onload = function() {
var anchors = document.getElementsByTagName('a');
for(var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if(/\bhohoho\b/).match(anchor.className)) {
anchor.onclick = function() {
alert('ho ho ho');
}
}
}
}
</script>
如果你愿意使用 jQuery,那么你可以对所有锚点执行以下操作:
<script>
$(document).ready(function() {
$('a').click(function() {
alert('ho ho ho');
});
});
</script>
这是一段 jQuery 代码,只针对具有特定类别的锚点进行应用:
<script>
$(document).ready(function() {
$('a.hohoho').click(function() {
alert('ho ho ho');
});
});
</script>
document.body.onclick
),然后检查被点击的元素 (event.srcElement
/e.target
) 是否有一个指定的类名:document.body.onclick = function(e) { //when the document body is clicked
if (window.event) {
e = event.srcElement; //assign the element clicked to e (IE 6-8)
}
else {
e = e.target; //assign the element clicked to e
}
if (e.className && e.className.indexOf('someclass') != -1) {
//if the element has a class name, and that is 'someclass' then...
alert('hohoho');
}
}
或者一个更简洁的版本:
document.body.onclick= function(e){
e=window.event? event.srcElement: e.target;
if(e.className && e.className.indexOf('someclass')!=-1)alert('hohoho');
}
document.body.onclick
的问题,但是似乎 document.onclick
运行良好。 - Skippy le Grand Gourou你可以用jQuery来实现。
$('.myClass').click(function() {
alert('hohoho');
});
无法通过CSS完成,因为CSS只能改变呈现方式(例如,只有JavaScript才能使警报弹出)。我强烈建议您查看一个名为jQuery的JavaScript库,因为它可以轻松完成这样的操作:
$(document).ready(function(){
$("a").click(function(){
alert("hohoho");
});
});
根据JCOC611的建议:
window.onload = function() {
let elements = document.getElementsByClassName("someClassName");
for(let i = 0; i < elements.length; i++) {
elements[i].onclick = function () {
alert("Clicked in an element of the class.");
}
}
};
以下是我的CSS解决方案,完全不使用JavaScript
HTML:
<a href="#openModal">Open Modal</a>
<div id="openModal" class="modalDialog">
<div> <a href="#close" title="Close" class="close">X</a>
<h2>Modal Box</h2>
<p>This is a sample modal box that can be created using the powers of CSS3.</p>
<p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
</div>
</div>
CSS:
.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalDialog:target {
opacity:1;
pointer-events: auto;
}
.modalDialog > div {
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}
.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}
.close:hover {
background: #00d9ff;
}
我相信它会像对我一样对你起作用。
$(".myCssClass").click(function() { alert("hohoho"); });
如果问题标题中涉及到“类”,答案是getElementsByClassName
:
var hrefs = document.getElementsByClassName("YOUR-CLASS-NAME-HERE");
for (var i = 0; i < hrefs.length; i++) {
hrefs.item(i).addEventListener('click', function(e){
e.preventDefault(); /*use if you want to prevent the original link following action*/
alert('hohoho');
});
}
document.getElementsByClassName("class")
。 - JCOC611getElementsByClassName
,因为它在一些主流浏览器上无法正常工作。 - Alex VidalgetElementsByClassName
:https://caniuse.com/getelementsbyclassname - Fanky