简体   繁体   English

如何将鼠标悬停在文本区域的每个单词上来设置文本装饰下划线

[英]how to set text-decoration underline on hover over text area's each word

I have to make a underline on each word in textarea when user hover over it after on click function on a button how can i do it? 当用户在按钮上单击功能后将鼠标悬停在文本区域上的每个单词上时,我必须在其上划下划线。

 $('#popup_button').on("click", function(){ $('#my-content').css('cursor', 'help' ); $('#my-content span').hover(function() { $('#my-content span').css("text-decoration", "underline"); }); }); 

It's a bit tricky and you need to use JavaScript to accomplish what you want. 这有点棘手,您需要使用JavaScript来完成所需的操作。

I suggest using both Jquery and Lettering.js 我建议同时使用JqueryLettering.js

You need also to use a contenteditable element instetad of a textarea. 您还需要使用textarea的contenteditable元素instetad。 Unfortunately a contenteditable element cannot be used inside a form. 不幸的是,不能在表单内部使用contenteditable元素。 So, before submitting your form you will have to read the value of the contenteditable element, strip HTML tags and set the value of a hidden textarea to that clean value. 因此,在提交表单之前,您将必须读取contenteditable元素的值,剥离HTML标签并将隐藏的textarea的值设置为该干净值。

Following you can find HTML, CSS and JS working code. 接下来,您可以找到HTML,CSS和JS工作代码。

HTML: HTML:

<div id="my-content" contenteditable="true"></div>
<form action="some-page.php" onsubmit="return getContent()">
    <textarea id="my-textarea" style="display:none"></textarea>
    <input type="submit" />
</form>
<span id="popup_button">click me!</span>

CSS: CSS:

#my-content {
  border: 1px solid #ccc;
  min-width: 300px;
  min-height: 100px;
  padding: 10px;
}

#my-content:hover span {
  text-decoration: underline;
}


#my-content.clicked:hover span {
  text-decoration: none;
}

JS: First import both jQuery and Lettering.js: JS:首先导入jQuery和Lettering.js:

  • https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js
  • https://cdnjs.cloudflare.com/ajax/libs/lettering.js/0.7.0/jquery.lettering.min.js https://cdnjs.cloudflare.com/ajax/libs/lettering.js/0.7.0/jquery.lettering.min.js

    function getContent(){ $("#my-textarea").val($("#my-content").attr("aria-label")); 函数getContent(){$(“#my-textarea”)。val($(“#my-content”)。attr(“ aria-label”))); } }
      function setEndOfContenteditable(contentEditableElement) { var range,selection; range = document.createRange();//Create a range (a range is a like the selection but invisible) range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start selection = window.getSelection();//get the selection object (allows you to change selection) selection.removeAllRanges();//remove any selections already made selection.addRange(range);//make the range you have just created the visible selection } elem = document.getElementById('my-content'); elem.addEventListener("keypress", function(evt) { var _evt = evt; setTimeout(function(){ _evt = _evt || window.event; var charCode = _evt.keyCode || _evt.which; //if(charCode == 32) { $("#my-content").lettering('words'); setEndOfContenteditable(elem); //} }, 10, _evt); }, false); $('#popup_button').on("click", function(){ $('#my-content').css('cursor', 'help' ); $('#my-content').addClass('clicked'); $('#my-content span').hover(function() { $('#my-content span').css("text-decoration", "none"); $(this).css("text-decoration", "underline"); }); }); 

Here a working Codepen example. 这是一个有效的Codepen示例。

Hope this will help you. 希望这会帮助你。

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

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