gpt4 book ai didi

jquery - 单击一次后阻止单击,直到用户再次单击另一个按钮

转载 作者:行者123 更新时间:2023-12-01 04:34:35 25 4
gpt4 key购买 nike

我制作了一个 contenteditable div 和一个按钮“趋势”,当用户单击按钮时,它会更改光标以提供帮助,并且每当用户右键单击或双击 contenteditable div 内的任何文本时,该文本将显示在另一个文本中div,这一切只有在点击趋势按钮后才会发生。当用户选择文本时,他必须再次单击趋势按钮才能选择另一个文本。基本上一次一个文本。但在我的多次双击代码中,单击趋势按钮后会选择多个文本。单击趋势按钮后,我必须一次选择一个文本,要选择另一个文本,用户必须再次单击趋势按钮,然后双击文本。

这是我的代码,以便更好地理解。

$('#popup_button').on("click", function(e){
e.preventDefault();
$("#textarea").css("cursor", "help");
$("#textarea").mousedown(function(e){
if (e.which === 3){
e.preventDefault();
document.getElementById("textarea").addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);
var text = getSelectionText(e);
//console.log(text);
$('.show_popupmenu').css('display', 'block');
$('.show_popupmenu').css('left',e.pageX); // <<< use pageX and pageY
$('.show_popupmenu').css('top',e.pageY);
$('.show_popupmenu').css('display','inline');
$(".show_popupmenu").css("position", "absolute");
}
if (e.which === 1) {
$('.show_popupmenu').css('display', 'none');
}
});
$("#textarea").dblclick(function(e){
e.preventDefault();
$('#textarea').css('cursor', 'auto');
$('.show_popupmenu').css('display', 'none');
var text = getSelectionText();
//console.log(text);
if(text !== ''){
//console.log(text);
$(".selected_trend").append('<div class="trends" id="trend'+text+'" data-text="'+text+'" style="width:auto; height:auto; display:inline-block;">'+text+'<p class="removetrend" data-id="'+text+'" style="display:inline-block; cursor:pointer;">&times</p></div>');
}
})
});
$(document).on('click','.removetrend', function(e){
e.preventDefault();
$(this).parent().remove();
});
function getSelectionText(){
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
#textarea{
height:100px;
border:1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
</head>
<body>
<div contenteditable="true" data-maxlength="50" id="textarea" class="editable" data-placeholder="Type here..."></div>
<div class="selected_trend"></div>
<div class="popupbtn">
<button id="popup_button">Select Trend</button>
</div>
</body>
</html>

最佳答案

jquery 将是这样的:

    <script type="text/javascript">

$('#popup_button').on("click", function(e){
$("#popup_button").attr('data-clicked',true);
e.preventDefault();
$("#textarea").css("cursor", "help");
$("#textarea").mousedown(function(e){
if (e.which === 3){
e.preventDefault();
document.getElementById("textarea").addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);
var text = getSelectionText(e);
//console.log(text);
$('.show_popupmenu').css('display', 'block');
$('.show_popupmenu').css('left',e.pageX); // <<< use pageX and pageY
$('.show_popupmenu').css('top',e.pageY);
$('.show_popupmenu').css('display','inline');
$(".show_popupmenu").css("position", "absolute");
}
if (e.which === 1) {
$('.show_popupmenu').css('display', 'none');
}
});


$("#textarea").dblclick(function(e){
e.preventDefault();



$('#textarea').css('cursor', 'auto');
$('.show_popupmenu').css('display', 'none');
var text = getSelectionText();

//console.log(text);


if(text !== ''){


if($("#popup_button").attr('data-clicked')=='true'){




//console.log(text);
$(".selected_trend").append('<div class="trends" id="trend'+text+'" data-text="'+text+'" style="width:auto; height:auto; display:inline-block;">'+text+'<p class="removetrend" data-id="'+text+'" style="display:inline-block; cursor:pointer;">&times</p></div>');
$("#popup_button").attr('data-clicked',false);
}

}



});

});
$(document).on('click','.removetrend', function(e){
e.preventDefault();
$(this).parent().remove();
});
function getSelectionText(){
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
</script>

关于jquery - 单击一次后阻止单击,直到用户再次单击另一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com