[英]jquery global variable with value window.getSelection() becomes empty
In one function want to set variable to be able to use in other functions. 一个功能中要设置变量就可以在其他功能中使用。 Value of the variable is
window.getSelection()
变量的值为
window.getSelection()
What i did: 我做了什么:
1) selected some text inside div contenteditable
1)在
div contenteditable
选择一些可编辑的文本
2) clicked on class="fa-link"
. 2)点击
class="fa-link"
。 Starts code that assigns value of window.getSelection()
to variable global_variable
启动将
window.getSelection()
值分配给变量global_variable
3) click anywhere else in page, for example in id="ahref_name"
. 3)单击页面中的其他任何位置,例如
id="ahref_name"
。 Value of global_variable
becomes empty. global_variable
值变为空。
Why global_variable
becomes empty? 为什么
global_variable
变为空? I did not started code that gives value to global_variable
. 我没有启动为
global_variable
代码。 So global_variable
must keep value... like some_test_var
. 因此,
global_variable
必须保留值...类似于some_test_var
。
Placed code here https://jsfiddle.net/xpvt214o/565926/ 将代码放在这里https://jsfiddle.net/xpvt214o/565926/
And here is the code: 这是代码:
Html HTML
<div contenteditable>To select something</div>
<button class="fa-link">fa-link</button>
<div id="contenteditable_menu_ahref" style="display:none" >
<input type="text" id="ahref_name" placeholder="Ahref name" />
</div>
jQuery jQuery的
var global_variable = '';
var some_test_var = '';
$(document).on('click', '.fa-link', function(){
console.log ('fa-link clicked');
$("#contenteditable_menu_ahref").css("display", "block");
if (window.getSelection) {
global_variable = window.getSelection();
console.log( 'global_variable 01 ' + global_variable );
some_test_var = 'some test var';
$("#ahref_name").val(global_variable);
}
});
$(document).click(function() {
console.log( "global_variable 02 " + global_variable + ' / some_test_var ' + some_test_var );
});
Try this: window.getSelection().getRangeAt(0).cloneContents();
试试这个:
window.getSelection().getRangeAt(0).cloneContents();
. 。 The issue is that you are receiving an object and that object is updated every time you select a different part of the DOM.
问题是您正在接收一个对象,并且每次选择DOM的不同部分时都会更新该对象。 By calling
getSelection().getRangeAt(0).cloneContents()
you'll store a copy of that selection in you variable. 通过调用
getSelection().getRangeAt(0).cloneContents()
您可以将该选择的副本存储在变量中。
The object is replaced when you click on the button. 当您单击按钮时,将替换该对象。 So instead of click, add a mouseup event on selection and store the selected value in the global variable
因此,不要单击,而是在选择上添加mouseup事件并将所选值存储在全局变量中
$("#contenteditable_menu_ahref").mouseup( function(){
if (window.getSelection) {
global_variable = window.getSelection();
}
});
$(document).on('click', '.fa-link', function(){
console.log ('fa-link clicked',global_variable);
});
Your problem is that: 您的问题是:
if (window.getSelection)
will also return true because it check that a function called 'window.getSelection' exists (which it does in all modern browsers. 也会返回true,因为它会检查是否存在一个名为“ window.getSelection”的函数(该函数在所有现代浏览器中均会执行)。
You should simply call: 您应该简单地致电:
if (window.getSelection.toString())
That will only report true if selection is non-empty. 仅当选择为非空时,才报告true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.