javascript - 更改 div 中跨度的可见性和大小分成两行

标签 javascript html css

我有一个带有两个 + 和 - 按钮和一个隐藏输入的 div。当我点击按钮时,输入中的值应该增加或减少并且输入应该变得可见。 问题是,根据缩放比例以及可能在 Firefox 和 Chrome 上的不同,按钮和输入分成三行。如何保持按钮和输入水平对齐?

<!DOCTYPE html>

<html>
<head>
    <script> 
    function showInput(){
        input=document.getElementById("input")
        div=document.getElementById("mydiv")
        input.style.width="40px";
        div.style.width="90px";
        input.style.visibility = "visible";
    };
    function change(increment){
        input=document.getElementById("input")
        input.value=Number(input.value)+increment;
        input.style.visibility='visible';
        console.log(input.value)
    }

    </script>
</head>
<body>
    <a href="https://stackoverflow.com/questions/3977596/how-to-make-divs-in-html5-draggable-for-firefox">I will check this to make it draggable in firefox</a>
    <div id="mydiv" style="position: absolute; left: 450px; top: 2px; background-color: lightgrey; padding: 1px;" draggable
="true">
        <span>
            <button id="btnm" onclick="showInput();change(-1);">-</button>
            <button id="btnp" onclick="showInput();change(+1);">+</button>
            <input id="input" value=1 style="visibility: hidden; width: 4px;">
        </span>
</div>
</body>
</html>

最佳答案

因为您使用的是 left 属性,所以 div 将在较小的设备上被推离页面。您可以改用 right 属性,并在 span 上应用 white-space: no-wrap。这将导致:

<!DOCTYPE html>

<html>
<head>
    <script> 
    function showInput(){
        input=document.getElementById("input")
        div=document.getElementById("mydiv")
        input.style.width="40px";
        div.style.width="90px";
        input.style.visibility = "visible";
    };
    function change(increment){
        input=document.getElementById("input")
        input.value=Number(input.value)+increment;
        input.style.visibility='visible';
        console.log(input.value)
    }

    </script>
</head>
<body>
    <a href="https://stackoverflow.com/questions/3977596/how-to-make-divs-in-html5-draggable-for-firefox">I will check this to make it draggable in firefox</a>
    <div id="mydiv" style="position: absolute; right: 0px; top: 2px; background-color: lightgrey; padding: 1px;" draggable
="true">
        <span style="white-space: nowrap">
            <button id="btnm" onclick="showInput();change(-1);">-</button>
            <button id="btnp" onclick="showInput();change(+1);">+</button>
            <input id="input" value=1 style="visibility: hidden; width: 4px;">
        </span>
</div>
</body>
</html>

另请记住,内联样式不是最佳实践!

关于javascript - 更改 div 中跨度的可见性和大小分成两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690211/

相关文章:

javascript - 在 ejs 循环中无法访问某些对象值

css - 我的网站在我放置这个不应该这样做的 div 之后在右边显示这个奇怪的空白

html - 悬停时顶部导航文本之间的垂直线闪烁

css - 我如何在 Nightmare.js 中找到 .click 的选择器?

javascript - Cheerio:如何获取祖 parent 下的所有标签

javascript - 运行 cypress 测试时,当我登录后访问新网址时,页面仅保留在当前网址上

javascript - 如何在 cookie 中存储触发的 jquery CSS 状态

javascript - 如何获取 JS 字符串并在 HTML5 中显示为预览?

jQuery 数据表 : Disabling sorting for specific columns

css - Google Pixel C 设备用户界面不会根据 CSS 选择器更改为横向模式