javascript - 传递给 javascript 函数 div 类参数

标签 javascript jquery html css

我在某个网站上工作。我有个问题。如何给 JS 函数赋予类名。 这是我的代码:

<div class="grid-item">
    <div id="info">
        <div class="glyphicon glyphicon-search" style="hidden: hidden;">
        </div>
        <img onmouseover="show(this)" onmouseout="hide(this)" src="'smiley.gif'" />
    </div>
</div>

我的 JS 函数是这样的

function show(x) {
    x.style.hidden= visible;
}

function hide(x) {
    x.style.hidden= hidden;
}

但是在onmouseoveronmouseout 中我不想使用this。我要显示或隐藏什么

<div class="glyphicon glyphicon-search" style="hidden: hidden;"></div>

来自 html。这个怎么做? 我的想法是给这个 div id,然后那个 id 传递给函数,但是如何做到这一点。

请记住,每张图片我都会有很多 div 标签 ()。

以及如何将该 div 定位在图像的右上角?

谢谢。

最佳答案

如果您正在使用 jQuery,请使用它。

function show(x) {
    $(x).prev('div.glyphicon.glyphicon-search').show();
}
function hide(x) {
    $(x).prev('div.glyphicon.glyphicon-search').hide();
}

但更喜欢做这样的事情。为这些图像添加一个类 笑脸

<img class="smiley" src="'smiley.gif'" />

然后

$('img.smiley').on('hover', show, hide);

function show() {
    $(this).prev('div.glyphicon.glyphicon-search').show();
}
function hide() {
    $(this).prev('div.glyphicon.glyphicon-search').hide();
}

或者使用mouseover/mouseout 事件和事件委托(delegate),

$(document)
    .on('mouseover', 'img.smiley', show)
    .on('mouseover', 'img.smiley', hide);

关于javascript - 传递给 javascript 函数 div 类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35605351/

相关文章:

javascript - 从该字符串中提取该特定数值的正则表达式是什么?

javascript - 在 ng-repeat 中调用一个函数

Javascript 在文本区域的括号之间移动文本

html - 100% 最小高度在 Opera 中有效,但在其他浏览器中无效

javascript - 如果未设置 cookie,如何为某些浏览器显示消息

javascript - IMacros:如何使用 javascript 提示

javascript - 为什么 Object 原型(prototype)方法会覆盖 JavaScript 中的 String 和 Number 原型(prototype)方法?

jquery - 选择下拉菜单周围的蓝色边框?

javascript - 如果 HTML5 本地浏览器数据库条目超出其固定大小

javascript - 使用 Javascript/PHP : Trying to display background image - logic wrong?