Accumulated code snippets/Tips

Source: Internet
Author: User

★Error in operations on Object elements in jquery1.7.2

In jquery1.7.2, an error is returned when you use the $ selector to operate the object element: the code is as follows:

$(‘object‘).hide();

It will be okay if you use native JS:

document.getElementsByTagName(‘object‘)[0].style.display = ‘none‘;

 

★Turn off the font of the button

When making a simple close button, we often use the letter X. To make it look more like a cross, you can set the font:

font-family: “Microsoft JhengHei”,”microsoft yahei”,Monaco,Menlo,Consolas,”Courier New”,monospace;

 

★The value of this is determined when the function is running.

<button id="btn1">button1</button><button id="btn2">button2</button>var obj = {    init : function(){        $(‘#btn1‘).click(this.alert);    },    init2 : function(){        var _this = this;        $(‘#btn2‘).click(function(){            _this.alert();        });    },    alert : function(){        alert(this);    }}         obj.init();obj.init2();

 

★Use regular expressions to split the number by kilobytes

With no decimal point:

"15000000".split("").reverse().join("").replace(/(\d{3})/g, "$1,").split("").reverse().join("");

Decimal point:

‘123123211312.333123‘.replace(/(?=(?!^)(?:\d{3})+(?:\.|$))(\d{3}(\.\d+$)?)/g,‘,$1‘);

 

★The background is translucent and the content is not transparent.

<Div> <p> opacity </P> </div> Div {Background: rgba (0.2, 0,) None repeat scroll! Important;/* implement FF background transparency, text opacity */Background: #000; filter: alpha (opacity = 20);/* Implement IE background transparency */width: 500px; height: 500px; color: # f30; font-size: 32px; font-weight: bold;} Div P {position: relative;}/* Implement IE text opacity */

Firefox uses rgba color to solve the problem that the sub-tag is semi-transparent, but IE does not support it very well.

Therefore, we can set a location attribute for tags that do not want to be transparent and solve the problem.

 

★Trigger the keydown event for the DIV Element

The reason why the DIV element cannot trigger keydown is that the graph cannot be focused. The processing method is to add the attribute tabindex to the div. The value of tabindex is an integer, indicating the order in which the elements obtain focus when pressing the tab key. When the value is-1, you cannot obtain the focus by pressing tab, but you can use JS Code to focus and blur. At the same time, you can still trigger the keydown event.

To enable the DIV to trigger the keydown event, you only need the following code:

<div tabindex="-1">hello world !</div>

After this attribute is added, the DIV will display a highlighted outer box, and the value of outline: none can be solved.

 

★Query URL parameters

function getParameterByName(name) {    var match = RegExp(‘[?&]‘ + name + ‘=([^&]*)‘)                    .exec(window.location.search);    return match && decodeURIComponent(match[1].replace(/\+/g, ‘ ‘));}

 

★On the mobile terminal, the elements bound with the click event will show a highlighted box when you click. The following code can be removed:

-webkit-tap-highlight-color:rgba(0,0,0,0);-webkit-tap-highlight-color: transparent; /* For some Androids */

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.