简体   繁体   English

onmouseover事件可更改另一个元素的css-两者都具有相同的动态ID

[英]onmouseover event to change css of another element -both with the same dynamic ID

I had image elements coming from a for loop, and an X in the upper right hand corner that deletes them from the DB. 我的图像元素来自for循环,右上角的X则将其从数据库中删除。

I decided to make it so that clicking the image would delete it instead. 我决定制作它,以便单击图像将其删除。 After that, I lost the X altogether, but it occured to me that it might be a better user experience if the X showed back up onmousover to indicate that the image would be deleted (still not sure if I'm going with opacity 0 -> opacity 1 or white -> red). 在那之后,我完全失去了X,但是我想到如果X显示onmousover来表示将删除该图像,可能会带来更好的用户体验(仍然不确定我是否使用不透明度0- >不透明度1或白色->红色)。

So here's my image: 所以这是我的形象:

<div class="close"><i id="<?php echo $images[$i]->id; ?>" class="fas fa-times"></i></div>
<img id="<?php echo $images[$i]->id; ?>" onclick="delete_img(this.id);" onmouseover="show_x(this.id)" src="https://drive.google.com/thumbnail?id=<?php echo $images[$i]->image_id; ?>&export=view&sz=w250">

And my jquery 而我的jQuery

function show_x(id)
{
  $("#" + id).css("color", "red");
}

Relevant CSS: 相关CSS:

.fas
{
    color: #ffffff;
}

It really feels like this should work, but it doesn't. 确实感觉应该可以,但是不能。 I know I'm getting into the function, because I can write: 我知道我要进入该函数,因为我可以写:

alert(id);

And it does exactly what I expect. 它完全符合我的期望。 I know jQuery is functioning properly, because the other jQuery function delete_img(id) works just fine. 我知道jQuery运行正常,因为其他jQuery函数delete_img(id)正常工作。

Any insight is appreciated. 任何见解均表示赞赏。 If this is a stupid question, please forgive me, because I know not what I do. 如果这是一个愚蠢的问题,请原谅我,因为我不知道我在做什么。

I also tried giving them different ID's..something like adding "x_" to the Font Awesome div ID, and ammending the jQuery like this: 我还尝试给它们提供不同的ID。例如在Font Awesome div ID中添加“ x_”,然后像这样修改jQuery:

function show_x(id)
{
  $("#x_" + id).css("color", "red");
}

But that didn't work either. 但这也不起作用。

Taplar, see this: Taplar,请参见:

<i class="fas fa-times <?php echo $images[$i]->id; ?>" style="color: white;"></i>

and

function show_x(id)
{

  $("."+id).css("color", "red");
}

Apologies for making invalid web markup, but this also doesn't seem to work. 做出无效的Web标记的歉意,但这似乎也不起作用。

You could use CSS to target the close icon rather relying on jQuery. 您可以使用CSS来定位关闭图标,而不是依靠jQuery。

If you ditch the mouseover function, and modify the gallery code a bit so the delete_img() function is on a tag containing the img , like an anchor tag for example <a href="#"> . 如果放弃鼠标悬停功能,并稍微修改图库代码,则delete_img()函数位于包含img的标签上,例如锚标签,例如<a href="#"> This way you can achieve revealing the delete icon just using CSS. 这样,您可以仅使用CSS来显示删除图标。

You may have to modify your delete_img() function to accommodate. 您可能需要修改delete_img()函数以适应。

See example below... 请参见下面的示例...

 a { display: block; position: relative; float: left; } a > .fas { transition: opacity 0.15s ease-in-out; opacity: 0; position: absolute; top: 20px; left: 20px; color: white; z-index: 2; } a:hover > .fas { opacity: 1; } a:after { content: ""; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background: black; opacity: 0; } a:hover:after { opacity: .5; } img { display: block; } 
 <a id="image-1" href="#" onclick="delete_img(this.id);" > <i class="fas fa-times">close icon</i> <img src="https://i.imgur.com/JJLSCq0.png"/> </a> 

Just remember to add event.preventDefault(); 只记得添加event.preventDefault(); on the delete_img() function. delete_img()函数上。 I would also avoid using the onclick in your html. 我也将避免在HTML中使用onclick and just write it in your script. 并在脚本中编写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM