代码之家  ›  专栏  ›  技术社区  ›  Antoine Pelletier

单击时选择(突出显示)文本框的内容

  •  0
  • Antoine Pelletier  · 技术社区  · 6 年前

    使用最新版本的ASP.NETMVC,我尝试将事件侦听器附加到文本框,以便在单击时突出显示文本框的内容。

    @Html.TextBoxFor(t => t.item1, new { onclick= "$(this).blur();" })
    

    但当我点击文本框时,它会立刻失去焦点,而不会在浏览器控制台中留下任何错误。

    我也试过JQuery的 onfocus onmouseup 事与愿违。

    单击文本框时,如何突出显示文本框的内容?

    3 回复  |  直到 6 年前
        1
  •  5
  •   Ramon de Vries    6 年前

    要选择选定文本框中的所有文本,请单击,

    代码如下: @Html.TextBoxFor(t => t.item1, new { onclick= "$(this).select();" })

        2
  •  4
  •   Alfredo A.    6 年前

    阅读文档 jQuery Blur method documentation

    将事件处理程序绑定到“blur”JavaScript事件,或在元素上触发该事件。

    现在读一下 JavaScript blur event documentation

    onblur事件发生在对象失去焦点时。

    所以,基本上,在您的示例中,您是说您的组件在单击时应该失去焦点,这正是正在发生的事情

    如果你想在视觉上 你的元素,然后你可以尝试这样的东西

    .blurred{
      -webkit-filter: blur(5px);
      -moz-filter: blur(5px);
      -o-filter: blur(5px);
      -ms-filter: blur(5px);
      filter: blur(5px);
    }
    div{
      width: 100px;
        height: 100px;
        background-color: #ccc;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <!-- If what you want is to blur the element -->
    <input type="text" value="click me to blur me" onclick="$(this).addClass('blurred')"/>
    
    <hr/>
    
    <!-- If what you want is to select all the content -->
    <input type="text" value="click me to select all the data" onclick="$(this).select()"/>
        3
  •  2
  •   eyllanesc Yonghwan Shin    6 年前

    正如我所理解的“模糊”在u方面,是文字的颜色应该改为浅灰色。

    @Html.TextBoxFor(t => t.item1, new { onclick= "$(this).css('color','#ddd');" })