gpt4 book ai didi

Javascript:选择时防止下拉菜单打开

转载 作者:行者123 更新时间:2023-12-03 05:42:14 24 4
gpt4 key购买 nike

我想防止在选择单元格范围时打开下拉菜单。我知道,我可以添加一个 onmousedown 事件并调用 event.preventDefault()

问题是,这也会禁用单击事件的下拉菜单。有没有办法区分范围的选择(鼠标被按下并没有立即释放)和单击单个单元格(鼠标被按下并释放)?我也尝试过 onselectstart 但在这种情况下它对我没有帮助。

这里有一个小演示: https://jsfiddle.net/vqsv99t4/2/

最佳答案

我已更新您的代码以根据您的需要工作。

  1. 禁用文本选择:从 Javascript 删除到 CSS

    .noselect {
    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Chrome/Safari/Opera */
    -khtml-user-select: none; /* Konqueror */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* Internet Explorer/Edge */
    user-select: none; /* Non-prefixed version, currently
    not supported by any browser */
    }

    来源:https://stackoverflow.com/a/4407335/1957036

  2. 停止来自 <td> 的事件气泡至document :event.stopPropagation()将停止将事件冒泡到文档 mousemove()mouseup()听众

  3. 逻辑:存储开始选择的第一个单元格(单元格上的 mousedown() 事件)。然后,检查选择已停止的单元格(mouseup()事件)。

  4. 修改了 HTML 以更好地使用。

  5. 已添加data-单元格上用于存储行列信息的属性

更新于2016年11月11日

  • 添加了 jQuery-UI Selectmenu 替换<select>

    Reason: <select> dropdown can't be triggered using JavaScript/JQuery.

  • //console.clear();

    var BUTTON_LEFT = 1,
    BUTTON_RIGHT = 2;

    var startCell = null;


    (function() {

    $("#select").selectmenu();

    $(".mouse-event-cell")
    .on('mousedown', onMouseDown)
    .on('mouseup', onMouseUp)
    .on('mousemove', onMouseOver);
    $(document)
    .on('mousemove', onDocMouseOver)
    .on('mouseup', onDocMouseUp);
    })();

    function onDocMouseOver(e) {
    if (e.buttons !== BUTTON_LEFT) {
    startCell = null;
    clearFill();
    }
    }

    function onDocMouseUp(e) {
    if (e.buttons !== BUTTON_LEFT) {
    startCell = null;
    clearFill();
    }
    }

    function onMouseDown(e) {
    isInsideCell = true;

    if (startCell === null)
    startCell = e.currentTarget;
    };

    function onMouseOver(e) {
    if (startCell !== null) {
    fill(startCell, e.currentTarget, 'region');
    }
    e.stopPropagation();
    }

    function onMouseUp(e) {
    var endCell = e.currentTarget;

    if (startCell !== null) {
    fill(startCell, endCell, 'selected');
    }

    startCell = null;
    e.stopPropagation()
    }

    function fill(startCell, endCell, classToAdd) {
    var col0 = startCell.dataset['column'],
    row0 = startCell.dataset['row'],
    col1 = endCell.dataset['column'],
    row1 = endCell.dataset['row'],
    colMin = Math.min(col0, col1),
    colMax = Math.max(col0, col1),
    rowMin = Math.min(row0, row1),
    rowMax = Math.max(row0, row1);

    clearFill();

    if (startCell === endCell) {
    console.log('same-cell');
    } else {
    console.log('range-of-cell');
    }

    //console.log(startCell, endCell);


    for (var itCol = colMin; itCol <= colMax; itCol++) {
    for (var itRow = rowMin; itRow <= rowMax; itRow++) {
    $('#codexpl .mouse-event-cell#cell_' + itRow + '_' + itCol).addClass(classToAdd);
    }
    }
    }

    function clearFill() {
    $('#codexpl .mouse-event-cell').removeClass('selected').removeClass('region');
    }
    .ui-selectmenu-button.ui-button {
    width: 5em;
    }
    select {
    width: auto;
    }
    table {
    border-collapse: separate;
    }
    td.mouse-event-cell {
    border: 1px solid #ddd;
    padding: 2px 10px;
    height: 30px;
    /*To disable the text selection*/
    -webkit-touch-callout: none;
    /* iOS Safari */
    -webkit-user-select: none;
    /* Chrome/Safari/Opera */
    -khtml-user-select: none;
    /* Konqueror */
    -moz-user-select: none;
    /* Firefox */
    -ms-user-select: none;
    /* Internet Explorer/Edge */
    user-select: none;
    /* Non-prefixed version, currently
    not supported by any browser */
    }
    td.selected {
    background-color: goldenrod;
    color: white;
    }
    td.region {
    border-style: dashed;
    border-color: #BBB;
    }
    <!DOCTYPE html>
    <html>

    <head>
    <meta name="description" content="selecting multiple cells">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>JS Bin</title>

    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
    <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
    <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

    </head>

    <body>
    <table id="codexpl">
    <tr>
    <th>#</th>
    <th>Columna</th>
    <th>Relative</th>
    <th>Isso</th>
    </tr>
    <tr>
    <td id="cell_0_0" data-row="0" data-column="0" class='mouse-event-cell'>
    <select id="select">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
    </select>
    </td>
    <td id="cell_0_1" data-row="0" data-column="1" class='mouse-event-cell'>cell[0][1]</td>
    <td id="cell_0_2" data-row="0" data-column="2" class='mouse-event-cell'>cell[0][2]</td>
    <td id="cell_0_3" data-row="0" data-column="3" class='mouse-event-cell'>cell[0][3]</td>
    <td id="cell_0_4" data-row="0" data-column="4" class='mouse-event-cell'>cell[0][4]</td>
    </tr>
    <tr>
    <td id="cell_1_0" data-row="1" data-column="0" class='mouse-event-cell'>cell[1][0]</td>
    <td id="cell_1_1" data-row="1" data-column="1" class='mouse-event-cell'>cell[1][1]</td>
    <td id="cell_1_2" data-row="1" data-column="2" class='mouse-event-cell'>cell[1][2]</td>
    <td id="cell_1_3" data-row="1" data-column="3" class='mouse-event-cell'>cell[1][3]</td>
    <td id="cell_1_4" data-row="1" data-column="4" class='mouse-event-cell'>cell[1][4]</td>
    </tr>
    <tr>
    <td id="cell_2_0" data-row="2" data-column="0" class='mouse-event-cell'>cell[2][0]</td>
    <td id="cell_2_1" data-row="2" data-column="1" class='mouse-event-cell'>cell[2][1]</td>
    <td id="cell_2_2" data-row="2" data-column="2" class='mouse-event-cell'>cell[2][2]</td>
    <td id="cell_2_3" data-row="2" data-column="3" class='mouse-event-cell'>cell[2][3]</td>
    <td id="cell_2_4" data-row="2" data-column="4" class='mouse-event-cell'>cell[2][4]</td>
    </tr>
    <tr>
    <td id="cell_3_0" data-row="3" data-column="0" class='mouse-event-cell'>cell[3][0]</td>
    <td id="cell_3_1" data-row="3" data-column="1" class='mouse-event-cell'>cell[3][1]</td>
    <td id="cell_3_2" data-row="3" data-column="2" class='mouse-event-cell'>cell[3][2]</td>
    <td id="cell_3_3" data-row="3" data-column="3" class='mouse-event-cell'>cell[3][3]</td>
    <td id="cell_3_4" data-row="3" data-column="4" class='mouse-event-cell'>cell[3][4]</td>
    </tr>
    <tr>
    <td id="cell_4_0" data-row="4" data-column="0" class='mouse-event-cell'>cell[4][0]</td>
    <td id="cell_4_1" data-row="4" data-column="1" class='mouse-event-cell'>cell[4][1]</td>
    <td id="cell_4_2" data-row="4" data-column="2" class='mouse-event-cell'>cell[4][2]</td>
    <td id="cell_4_3" data-row="4" data-column="3" class='mouse-event-cell'>cell[4][3]</td>
    <td id="cell_4_4" data-row="4" data-column="4" class='mouse-event-cell'>cell[4][4]</td>
    </tr>
    </table>
    </body>

    </html>

    关于Javascript:选择时防止下拉菜单打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40481926/

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