javascript - 转换为 Jquery

标签 javascript jquery

<分区>

今天早上我正在尝试做最简单的事情,但我的大脑不会参与。在过去的几周里,我一直在学习基本的 javascript 和 Jquery。我有一段 javascript 代码,我想将其转换为 jquery,我希望我的 javascript 不引人注目。

代码片段是

<p>
    This is the main content. To display a lightbox click <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">here</a>
</p>
<div id="light" class="white_content">
    This is the lightbox content. 
    <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">Close</a>
</div>
<div id="fade" class="black_overlay"></div>

我正在努力使这个灯箱更高效一些,但今天似乎无法解决如何转换的问题。感谢任何帮助

最佳答案

javascript:void(0); 的行为可以通过 return false 重现,当放在 click 中时绑定(bind)。

document.getElementById('light') 可以重写为 $("#light")

您需要向链接添加类或 ID 才能定位它们。

$(document).ready(function(){
    $("a").click(function() { // replace this selector as appropriate
        // do stuff here - lookup css in the JQuery docs
        return false; // prevents the link's default action - see also e.preventDefault() in the docs
    });
});

关于javascript - 转换为 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797988/

相关文章:

jquery - jQuery AJAX 拉取后更新 URL

javascript - 页面滚动时更改标题

javascript - 开/关导航面板(页面间缓存)

javascript - 如何调试 $ionicView.beforeEnter 和 $ionicView.enter 之间的时间

javascript - 使用 localstorage HTML5 保存表单

javascript - 如何选择溢出省略号的开始位置

jQuery "tagging"事件

jquery - 停止 jQuery 中所有事件的 ajax 请求

javascript - 如何在javascript中对类、函数和函数属性进行分类?

php - 定期从服务器端调用函数?