当焦点位于“内部”时如何更改元素/容器的样式 它?

发布于 2024-07-07 04:24:12 字数 841 浏览 7 评论 0原文

假设有这样的代码:

<div class="notSelected">
      <label>Name
          <input type="text" name="name" id="name" />
      </label> 
      <div class="description">
          Tell us what's your name to make us able to fake to be your friend 
          when sending you an email.
      </div>
</div>

现在假设表单的每个元素都有这样的代码(这只是一个示例)。 我想在以下情况下将样式从 notSelected 更改为 Selected:

  • 用户将焦点放在输入元素上
  • 用户将鼠标移到 notSelected div 上

当他更改焦点时,Selected div 应该再次变为 notSelected。

我想做这样的事情来增加所选 div 的文本大小。 无论如何,进行其他更改也可能很酷,所以我更愿意更改类属性。

在 JavaScript 中执行此类操作的最佳方法是什么? 有没有任何 JavaScript 框架可以帮助我做这件事? 因此,添加淡入淡出等效果会很容易...

我下载了 MooTools,但快速阅读了文档后,我不知道如何在没有任何表单 div 的特定 ID 的情况下执行此操作,但这是第一次我用它。 我使用任何其他框架都没有问题,但如果你建议一个,请写下我应该具体寻找什么。

Suppose to have a code like this:

<div class="notSelected">
      <label>Name
          <input type="text" name="name" id="name" />
      </label> 
      <div class="description">
          Tell us what's your name to make us able to fake to be your friend 
          when sending you an email.
      </div>
</div>

Now suppose I've something like this (it's just an example) for each element of a form.
I'd like to change the style from notSelected to Selected when:

  • User focus on the input element
  • User move the mouse over a notSelected div

When he change focus the Selected div should became notSelected again.

I'd like to do something like this to increment the size of the text of the selected div. Anyway it could be cool make other changes too so I'd prefer to change the class attribute.

What is the best way to do something like this in JavaScript? Is there any JavaScript framework that can boost me doing this thing? So it will be easy to add effects like fading etc...

I downloaded MooTools but with a fast read of the docs I did not see how to do this without having a specific ID for any of the forms div, but is the first time I use it. I've no problem using any other framework, but if you suggest one, please write also what should I look for specifically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寒尘 2024-07-14 04:24:12

对于这个问题还有一个纯 CSS 解决方案。 但是,它在 MSIE 6 中不起作用。从技术上讲,它的工作原理与 Tomalek 的解决方案类似,但它不使用 JavaScript 来切换元素的类,而是使用 CSS 来切换其样式:

.selectable { /* basic styles …  */ }
.selectable:hover { /* hover styles … */ }
.selectable:active { /* focus styles … */ }

There's also a pure CSS solution to this problem. However, it doesn't work in MSIE 6. Technically, it works similar to Tomalek's solution but instead of using JavaScript to toggle the element's class, it uses CSS to toggle its style:

.selectable { /* basic styles …  */ }
.selectable:hover { /* hover styles … */ }
.selectable:active { /* focus styles … */ }
戏舞 2024-07-14 04:24:12

我建议您查看 jQuery 来完成您的任务。 它非常容易学习并且快速产生良好的效果。 但仅凭你所描述的效果,纯 JavaScript 就足够了。

让你的 DIV 始终有一个名为“selectable”的类。 您可以稍后切换其他 CSS 类。 创建一个名为“selected”的 CSS 类并为其提供所需的外观。

<div class="selectable">  (=off) vs. <div class="selectable selected"> (=on)

然后将类似这样的内容添加到文档的脚本部分:

$(document).ready(function(){

  // handle mouseover and mouseout of the parent div
  $("div.selectable").mouseover(
    function() { 
      $(this).addClass("selected").addClass("mouseIsOver");
    }
  ).mouseout(
    function() { 
      $(this).removeClass("selected").removeClass("mouseIsOver");
    }
  );

  // handle focus and blur of the contained input elememt,
  // unless it has already been selected by mouse move
  $("div.selectable input").focus(
    function() {
      $(this).parents("div.selectable").not(".mouseIsOver").addClass("selected");
    }
  ).blur(
    function() {
      $(this).parents("div.selectable").not(".mouseIsOver").removeClass("selected");
    }
  );
});

这尚未经过测试,因此可能存在故障,但它会给您大致的了解从哪里开始。

PS:在鼠标移动时更改文本大小可能不是最好的想法。 它会导致重新排列页面布局,这对用户来说很烦人。

I would recommend a look at jQuery for your task. It is quite easy to learn and produces nice effects quickly. But your described effect alone, pure JavaScript would also be enough.

Make your DIVs always have a class called "selectable". You can toggle other CSS classes later on. Create a CSS class named "selected" and give it the desired look.

<div class="selectable">  (=off) vs. <div class="selectable selected"> (=on)

Then add something like this to the scripts section of your document:

$(document).ready(function(){

  // handle mouseover and mouseout of the parent div
  $("div.selectable").mouseover(
    function() { 
      $(this).addClass("selected").addClass("mouseIsOver");
    }
  ).mouseout(
    function() { 
      $(this).removeClass("selected").removeClass("mouseIsOver");
    }
  );

  // handle focus and blur of the contained input elememt,
  // unless it has already been selected by mouse move
  $("div.selectable input").focus(
    function() {
      $(this).parents("div.selectable").not(".mouseIsOver").addClass("selected");
    }
  ).blur(
    function() {
      $(this).parents("div.selectable").not(".mouseIsOver").removeClass("selected");
    }
  );
});

This is untested so there might be a glitch in it, but it will give you the general idea where to start.

P.S: Changing text size on mouse move may not be the best of all ideas. It leads to rearranging the page layout which is annoying for the user.

小帐篷 2024-07-14 04:24:12

@Tomalak:

为什么要查询 DOM 四次?

一个小的编辑,但是一个巨大的速度优势:

$("div.selectable").mouseover(function ()
{
    // ...
}).mouseout(function ()
{
    // ...
});

@Tomalak:

Why are you querying the DOM four times?

A small edit, but a huge speed-benefit:

$("div.selectable").mouseover(function ()
{
    // ...
}).mouseout(function ()
{
    // ...
});
懒猫 2024-07-14 04:24:12

这有点无关,但标签标签不包含输入标签。 您为 label 标签赋予一个“for”属性,该属性对应于输入元素的 ID。 例子,

<label for="username">Username</label>
<input type="text" name="username" value="Enter your username..." id="username" />

This is somewhat unrelated, but the label tag does not enclose the input tag. You give the label tag a "for" attribute which corresponds to the ID of the input element. Example,

<label for="username">Username</label>
<input type="text" name="username" value="Enter your username..." id="username" />
吖咩 2024-07-14 04:24:12

还有一个与原始问题无关的答案,但是... jQuery 还有一种替代语法,用于执行鼠标悬停/鼠标悬停操作,称为悬停。

$(element).hover(function(){ /* mouseover */ }, function(){ /* mouseout */ });

例如,

$('ul#nav li').hover(function() {
    $(this).addClass('highlight');
}, function() {
    $(this).removeClass('highlight');
});

对于那些双 $ 感到抱歉,我似乎不知道如何逃避这一点。

Yet another unrelated-to-the-original-question answer but... jQuery also has an alternative syntax for doing mouseover/mouseout stuff called hover.

$(element).hover(function(){ /* mouseover */ }, function(){ /* mouseout */ });

Example,

$('ul#nav li').hover(function() {
    $(this).addClass('highlight');
}, function() {
    $(this).removeClass('highlight');
});

Sorry for those double $'s, I can't seem to figure out how to escape that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文