简体   繁体   English

点击添加背景颜色

[英]Add background color on click

HI i have a dynamic li in my django template 我在我的django模板中有一个动态的li

<script type="text/javascript" language="javascript">
    function setSize(size) {
        document.getElementById('id_option1').value = size;
    }

    function notavailable(notavai) {
        alert('selected product is not available');
    }
</script>


{% for i in prosize %}
    {% if i.num_in_stock > 0 %}
        <li><a id="{{i.option1}}1" ref="javascript:setSize('{{i.option1}}')">{{i.option1}}</a></li>
    {% endif %}
{% endfor %}

i need to add the background * color * to active a link onclick i am already using a javascript function on a .Please suggest how can i do this 我需要添加背景 * 颜色 *来激活 链接 onclick已经在a上使用了javascript函数。请告诉我该如何做到这一点

Working jsFiddle Demo 工作jsFiddle演示

So, it's easy, in HTML markup you have: 所以,很容易,在HTML标记中你有:

<a class="order" id="{{i.option1}}" href="javascript:setSize('{{i.option1}}')">{{i.option1}}</a>
  • Note that I've changed the id . 请注意,我已经更改了id I deleted the 1 from at the end of it. 我从最后删除了1
  • Also the href attribute you wrote, was invalid. 您编写的href属性也是无效的。 I've correct it. 我纠正了。
  • Also, I've added a class to it for furthur usage. 另外,我已经为它添加了一个类以供更进一步使用。

And change your setSize function to this: 并将setSize函数更改为:

// because IE9 (and below) doesn't support for getElementsByClassName
// we use this funciton instead
// written by Jonathan Snook
// http://snook.ca/archives/javascript/your_favourite_1
function getElementsByClassName(node, classname) {
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function setSize(size) {
    // i just comment this line as i don't know what it doesyourself
    // document.getElementById('id_option1').value = size;

    // change the color the others to transparent
    var others = getElementsByClassName(document, 'order');
    for (var i = 0, l = others.length; i < l; i++) {
        others[i].style.backgroundColor = 'transparent';
    }

    // this line will change the background color of your element
    document.getElementById(size).style.backgroundColor = '#ff6600';
}

jQuery jQuery的

If you are using jQuery, there is no need to use javascript internally, let's jQuery handle all them for you. 如果您使用的是jQuery,则无需在内部使用javascript,让jQuery为您处理所有这些内容。 Write this code in your .js file: 将此代码写在.js文件中:

$(function () {
    // get al links and attach the `click` handler to them
    $('.order').on('click', function (e) {
        // prevent default behaviour of link
        e.preventDefault();

        // get size and do something with it
        var size = $(this).attr('data-size');
        $('#textbox').val(size);

        // change the color the others to transparent
        $('.order').css('background-color', 'transparent');

        // change the color of link
        $(this).css('background-color', '#ff6600');
    });
});

What you want should not be implemented in javascript or python but CSS. 你想要的不应该在javascript或python中实现,而是CSS。 You should use :active selector in CSS. 你应该在CSS中使用:active selector。

http://www.w3schools.com/cssref/sel_active.asp http://www.w3schools.com/cssref/sel_active.asp

in your loop, you can also bind the on click event and pass the id ...like 在你的循环中,你也可以绑定点击事件并传递id ...就像

{% for i in prosize %}
    {% if i.num_in_stock > 0 %}
        <li><a id="{{i.option1}}1" onclick='ChangeColor('+{{i.option1}}1+')' ref="javascript:setSize('{{i.option1}}')">{{i.option1}}</a></li>
    {% endif %}
{% endfor %}

function ChangeColor(id)
{
    $("#"+id).css("background","#fff");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM