不显眼但又可用的跨度按钮?

发布于 2024-09-30 05:28:55 字数 466 浏览 6 评论 0原文

我想使用 span 标签创建样式按钮,其作用或多或少类似于

在 jQuery 中将普通的 标记分配给 .click() 事件可以正常工作,但不能像正确的按钮一样工作。我不知道如何实现这一点,除了使用 标记,使用 #javascript: 链接来实现 href 属性。但这几乎违背了不引人注目的 JavaScript 的目的。另外,锚链接已经具有下划线文本和需要覆盖的颜色。

总而言之,如何才能实现一个不显眼的跨度按钮?不依赖锚点或实际的按钮输入标签。谢谢。

I want to create styled buttons using span tags, that act more or less like a <button> tag. Rapidly clicking the button shouldn't select any text inside or outside it's element, or be able to select text at all, and should also have a pointer cursor.

Assigning a plain <span> tag to a .click() event in jQuery will function fine, but not act like a proper button. I have no idea how to accomplish this except use an <a> tag instead, using # or a javascript: link for the href attribute. but that pretty much defeats the purpose of unobtrusive JavaScript. Plus, anchor links already have underlined text, and colors that need overriding.

All in all, how can I accomplish a unobtrusive span button? Without relying on anchors or an actual button input tag. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-10-07 05:28:55

更新

您可以使用这些样式来防止选择:

-webkit-user-select:none;
-moz-user-select:none;

在此处查看演示


注意< /em> 上面的内容仅适用于 webkit 和 mozilla 浏览器。


据我了解,jQuery 是为了最简单地做到不引人注目。你可以这样做:

$(function(){
    $('#span_id').click(function(){
      // your code here...
    });
});

注意标签是内联元素,你需要用使其块级 display:block 对其应用宽度高度 并使其看起来像一个按钮。

话虽如此,我想知道为什么你不能/不使用按钮来使你的代码在语义上有效。

Update

You can use these styles to prevent selection:

-webkit-user-select:none;
-moz-user-select:none;

See Demo Here


Please note that above will work only in webkit and mozilla browsers.


As far as I understand it, jQuery is made to go unobstrusive easiest. You can do like this:

$(function(){
    $('#span_id').click(function(){
      // your code here...
    });
});

Note that <span> tag is inline elements, you need to make it block-level with display:block to apply width and height to it and make it look like a button.

Having said that, I wonder why you can't/don't use a button instead and make your code semantically valid.

乜一 2024-10-07 05:28:55

这可能是一个疯狂的想法,但您可以使用按钮作为按钮。它们可以像其他东西一样设计。

This may be a crazy idea, but you could use buttons as buttons. They can be styled just like everything else can.

不寐倦长更 2024-10-07 05:28:55

“跨度按钮”

<span class="spanbutton" title="Span Button!" onclick="spanButtonFunction()">Span Button!</span>

样式:

.spanbutton {
    color:#000000;
    border:1px solid #000000;
    padding-left:8;
    padding-right:8px;
    border-radius:3px;
    cursor:pointer;
    font-family: arial;
    font-size:small;

    /*prevent selection*/
    -webkit-user-select:none;
    -moz-user-select:none;

    /* fallback */
    background-color:#EEEEEE;

    /* Safari 4-5, Chrome 1-9 */
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#CCCCCC),     to(#F4F4F4));

    /* Safari 5.1, Chrome 10+ */
    background: -webkit-linear-gradient(top, #F4F4F4, #CCCCCC);

    /* Firefox 3.6+ */
    background: -moz-linear-gradient(top, #F4F4F4, #CCCCCC);

    /* IE 10 */
    background: -ms-linear-gradient(top, #F4F4F4, #CCCCCC);

    /* Opera 11.10+ */
    background: -o-linear-gradient(top, #F4F4F4, #CCCCCC);
}

当然还有按钮功能的脚本。


此外,当您单击按钮时,如果您想对按钮“中键单击”的外观进行编码,jQuery 将提供帮助:

$(document).ready(function (){
    $(".spanbutton").bind("mousedown", function(e){
            buttondown(this);
    });
    $(".spanbutton").bind("mouseup", function(e){
            buttonup(this);
    });
    $(".spanbutton").bind("mouseout", function(e){
            buttonup(this);
    });
});

function buttondown(button) {
    button.className = "downbutton";
}

function buttonup(button) {
    button.className = "spanbutton";
}

以及“向下”按钮的更多样式:

.downbutton {
    color:#000000;
    border:1px solid #333333;
    padding-left:8px;
    padding-right:8px;
    border-radius:3px;
    cursor:pointer;
    font-family: arial;
    font-size:small;

    /*prevent selection*/
    -webkit-user-select:none;
    -moz-user-select:none;

    background-color:#BBBBBB;
}

如果您喜欢,请投票!这将激励我做出更多像这样的详细回应

The 'span button'

<span class="spanbutton" title="Span Button!" onclick="spanButtonFunction()">Span Button!</span>

The styling:

.spanbutton {
    color:#000000;
    border:1px solid #000000;
    padding-left:8;
    padding-right:8px;
    border-radius:3px;
    cursor:pointer;
    font-family: arial;
    font-size:small;

    /*prevent selection*/
    -webkit-user-select:none;
    -moz-user-select:none;

    /* fallback */
    background-color:#EEEEEE;

    /* Safari 4-5, Chrome 1-9 */
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#CCCCCC),     to(#F4F4F4));

    /* Safari 5.1, Chrome 10+ */
    background: -webkit-linear-gradient(top, #F4F4F4, #CCCCCC);

    /* Firefox 3.6+ */
    background: -moz-linear-gradient(top, #F4F4F4, #CCCCCC);

    /* IE 10 */
    background: -ms-linear-gradient(top, #F4F4F4, #CCCCCC);

    /* Opera 11.10+ */
    background: -o-linear-gradient(top, #F4F4F4, #CCCCCC);
}

And of course your script for the function of the button.


Additionally, when you click the button if you want to code the appearance of a button 'mid-click', jQuery will help :

$(document).ready(function (){
    $(".spanbutton").bind("mousedown", function(e){
            buttondown(this);
    });
    $(".spanbutton").bind("mouseup", function(e){
            buttonup(this);
    });
    $(".spanbutton").bind("mouseout", function(e){
            buttonup(this);
    });
});

function buttondown(button) {
    button.className = "downbutton";
}

function buttonup(button) {
    button.className = "spanbutton";
}

And some more styling for the 'down' button:

.downbutton {
    color:#000000;
    border:1px solid #333333;
    padding-left:8px;
    padding-right:8px;
    border-radius:3px;
    cursor:pointer;
    font-family: arial;
    font-size:small;

    /*prevent selection*/
    -webkit-user-select:none;
    -moz-user-select:none;

    background-color:#BBBBBB;
}

Vote if you like it! This will inspire me to do more in-detail responses like this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文