Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使工作Ctrl+;用星星替换文本的Z事件?_Javascript_Jquery_Css - Fatal编程技术网

Javascript 如何使工作Ctrl+;用星星替换文本的Z事件?

Javascript 如何使工作Ctrl+;用星星替换文本的Z事件?,javascript,jquery,css,Javascript,Jquery,Css,我有一个场景,我需要在文本区域用星号替换文本。。替换文本后,默认事件(Ctrl+Z)不起作用 JavaScript: $(document).ready(function () { closePopUp(); var newpost = $('#newpost'); $('#mytextarea').on('select', function (e) { var txtarea = document.getElementById("mytextarea")

我有一个场景,我需要在文本区域用星号替换文本。。替换文本后,默认事件(Ctrl+Z)不起作用

JavaScript:

$(document).ready(function () {
    closePopUp();
    var newpost = $('#newpost');
    $('#mytextarea').on('select', function (e) {
        var txtarea = document.getElementById("mytextarea");
        var start = txtarea.selectionStart;
        var finish = txtarea.selectionEnd;
        newpost.offset(getCursorXY(txtarea, start, 20)).show();
        newpost.find('div').text('replace with stars');
    });
});

const getCursorXY = (input, selectionPoint, offset) => {
    const {
        offsetLeft: inputX,
        offsetTop: inputY,
        } = input
    // create a dummy element that will be a clone of our input
    const div = document.createElement('div')
    // get the computed style of the input and clone it onto the dummy element
    const copyStyle = getComputedStyle(input)
    for (const prop of copyStyle) {
        div.style[prop] = copyStyle[prop]
    }
    // we need a character that will replace whitespace when filling our dummy element 
    // if it's a single line <input/>
    const swap = '.'
    const inputValue = input.tagName === 'INPUT' ? input.value.replace(/ /g, swap) : input.value
    // set the div content to that of the textarea up until selection
    const textContent = inputValue.substr(0, selectionPoint)
    // set the text content of the dummy element div
    div.textContent = textContent
    if (input.tagName === 'TEXTAREA') div.style.height = 'auto'
    // if a single line input then the div needs to be single line and not break out like a text area
    if (input.tagName === 'INPUT') div.style.width = 'auto'
    // create a marker element to obtain caret position
    const span = document.createElement('span')
    // give the span the textContent of remaining content so that the recreated dummy element 
    // is as close as possible
    span.textContent = inputValue.substr(selectionPoint) || '.'
    // append the span marker to the div
    div.appendChild(span)
    // append the dummy element to the body
    document.body.appendChild(div)
    // get the marker position, this is the caret position top and left relative to the input
    const { offsetLeft: spanX,offsetTop: spanY } = span
    // lastly, remove that dummy element
    // NOTE:: can comment this out for debugging purposes if you want to see where that span is rendered
    document.body.removeChild(div)
    // return an object with the x and y of the caret. account for input positioning 
    // so that you don't need to wrap the input
    return {
        left: inputX + spanX,
        top: inputY + spanY + offset,
    }
}
$(文档).ready(函数(){
closePopUp();
var newpost=$(“#newpost”);
$('#mytextarea')。在('select',函数(e)上{
var txtarea=document.getElementById(“mytextarea”);
var start=txtarea.selectionStart;
var finish=txtarea.selectionEnd;
offset(getCursorXY(txtarea,start,20)).show();
newpost.find('div').text('replace with stars');
});
});
常量getCursorXY=(输入、选择点、偏移)=>{
常数{
offsetLeft:inputX,
偏置:输入,
}=输入
//创建一个虚拟元素,它将是我们输入的克隆
const div=document.createElement('div')
//获取输入的计算样式,并将其克隆到虚拟元素上
const copyStyle=getComputedStyle(输入)
用于(复制样式的常量属性){
div.style[prop]=复制样式[prop]
}
//在填充虚拟元素时,我们需要一个字符来替换空白
//如果是单行线
常量交换='。'
常量inputValue=input.tagName=='input'?input.value.replace(//g,swap):input.value
//将div内容设置为文本区域的内容,直到选择为止
const textContent=inputValue.substr(0,selectionPoint)
//设置虚拟元素div的文本内容
div.textContent=textContent
如果(input.tagName=='TEXTAREA')div.style.height='auto'
//如果是单行输入,则div需要是单行,而不是像文本区域那样断开
如果(input.tagName=='input')div.style.width='auto'
//创建标记元素以获取插入符号位置
const span=document.createElement('span')
//为跨度指定剩余内容的textContent,以便重新创建的虚拟元素
//越近越好
span.textContent=inputValue.substr(selectionPoint)| |'。'
//将跨度标记附加到div
子类附件(span)
//将虚拟元素附加到主体
document.body.appendChild(div)
//获取标记位置,这是相对于输入的顶部和左侧插入符号位置
常量{offsetLeft:spanX,offsetTop:spanY}=span
//最后,删除该虚拟元素
//注意::如果您想查看呈现跨度的位置,可以出于调试目的对此进行注释
document.body.removeChild(div)
//返回带有插入符号x和y的对象。输入定位的帐户
//这样就不需要包装输入
返回{
左:inputX+spanX,
顶部:输入+跨距+偏移,
}
}
这是我的

我需要在执行“Ctrl+Z”时获取上一个文本


提前谢谢。

试试这个。它将保存最后一次替换并在Ctrl+Z上撤消它

但是,如果要保存多个替换,则需要更复杂的逻辑

var选择={};
函数撤消(e){
var evtobj=window.event?window.event:e;
if(evtobj.keyCode==90&&evtobj.ctrlKey&&selection.text){
evtobj.preventDefault();
var txtarea=document.getElementById(“mytextarea”);
var allText=txtarea.value;
var newText=allText.substring(0,selection.start)+selection.text+allText.substring(selection.finish,allText.length);
txtarea.value=newText;
}
}
函数getSel(){
//获取textarea>
var txtarea=document.getElementById(“mytextarea”);
//获取第一个选定字符的索引
var start=txtarea.selectionStart;
//获取最后选定字符的索引
var finish=txtarea.selectionEnd;
//获取所有文本
var allText=txtarea.value;
selection.text=allText.substring(开始、结束);
selection.start=开始;
selection.finish=完成;
//获取所选文本
var sel=Array(finish-start+1);
//追加文本;
var newText=allText.substring(0,开始)+sel+allText.substring(结束,allText.length);
txtarea.value=newText;
$('#newpost').offset({top:0,left:0}).hide();
}
函数closePopUp(){
$('#newpost').offset({top:0,left:0}).hide();
}
$(文档).ready(函数(){
closePopUp();
var newpost=$(“#newpost”);
$('#mytextarea')。在('select',函数(e)上{
var txtarea=document.getElementById(“mytextarea”);
var start=txtarea.selectionStart;
var finish=txtarea.selectionEnd;
offset(getCursorXY(txtarea,start,20)).show();
newpost.find('div').text(数组(finish-start+1).join(“*”);
}).on('input',()=>selection.text=null);
document.onkeydown=undo;
});
常量getCursorXY=(输入、选择点、偏移)=>{
常数{
offsetLeft:inputX,
偏置:输入,
}=输入
//创建一个虚拟元素,它将是我们输入的克隆
const div=document.createElement('div')
//获取输入的计算样式,并将其克隆到虚拟元素上
const copyStyle=getComputedStyle(输入)
用于(复制样式的常量属性){
div.style[prop]=复制样式[prop]
}
//在填充虚拟元素时,我们需要一个字符来替换空白
//如果是单行线
常量交换='。'
常量inputValue=input.tagName=='input'?input.value.replace(//g,swap):input.value
//将div内容设置为文本区域的内容,直到选择为止
const textContent=inputValue.substr(0,selectionPoint)
//设置虚拟元素div的文本内容
div.textContent=textContent
如果(input.tagName=='TEXTAREA')div.style.height='auto'
//如果是单行输入,则div需要是单行,而不是像文本区域那样断开
如果(input.tagName=='input')div.style.width='auto'
/