简体   繁体   English

如何在单击链接时更改内联css样式?

[英]How can i change an inline css style when clicking a link?

i have a form hidden with inline style="display: none;" 我有一个隐藏的内联style="display: none;"

how can i dinamicaly change this style to style="display: inline;" 我怎么能将这种风格改为style="display: inline;" when a link on the page is clicked? 当点击页面上的链接时?

Prety simple Prety简单

<a href="#" onclick="document.getElementById('myform').style.display = 'inline';">Click me</a>

Update 更新


jQuery is a lightweight JavaScript library that can do tons of cool stuff, with writing a very less script from the developers side. jQuery是一个轻量级的JavaScript库,可以做很多很酷的东西,从开发人员那边编写一个非常少的脚本。

First of all, I suggest you to read " How jQuery works? ", is has everything you need to get started using the jQuery. 首先,我建议你阅读“ jQuery如何工作? ”,它具有开始使用jQuery所需的一切。


I will explain the code I wrote in the fiddle. 我将解释我在小提琴中写的代码。

First of all the link & form 首先是链接和表单

<a id="linktotoggle" href="#">Click Me</a>
<form id="formtotoggle"></form>

Remember the id in the link and form above. 请记住上面链接和表单中的ID。 That is how we are going to select the element in the script just like what document.getElementById() does. 这就是我们如何选择脚本中的元素,就像document.getElementById()那样。

Lets hide the form by default 让我们默认隐藏表单

#formtotoggle { display: none; }

Now lets write the jquery 现在让我们编写jquery

$(document).ready(function() {
// ^ This is an event, which triggers once all the document is loaded so that the manipulation is always guaranteed to run.
   $("#linktotoggle").click(function() {
   // ^ Attach a click event to our link
        $("#formtotoggle").toggle();
        // ^ select the form and toggle its display

   });
});

Hope it is enough, to get you started. 希望它足够让你开始。

Bind an onclick event on the anchor, and set the form style to display:block , here's a js fiddle to help you going 在锚点上绑定一个onclick事件,并将表单样式设置为display:block ,这是一个帮助你前进的小提琴

http://jsfiddle.net/ZUgPv/1/ http://jsfiddle.net/ZUgPv/1/

You'll need to find some way of selecting the form using JavaScript, first; 首先,您需要找到一些使用JavaScript选择表单的方法; here's a function assuming that your form has an id attribute of myform : 这是一个函数,假设您的表单具有myformid属性:

function showForm() {
    document.getElementById('myform').style.display = 'inline';
}

Then, just bind that function to your link's click event. 然后,将该函数绑定到链接的click事件。 A quick and dirty way is to set the link's onclick attribute to showForm(); return false 一种快速而肮脏的方法是将链接的onclick属性设置为showForm(); return false showForm(); return false , but you'll probably want to do that in external JavaScript to separate your content and behaviour nicely. showForm(); return false ,但您可能希望在外部JavaScript中这样做,以便很好地分离您的内容和行为。

Hey Check this JQuery. 嘿检查这个JQuery。

For Show 对于秀

$("#lnk").click(function () {  
$("#result").removeAttr('Style');
$("#result").attr('Style','display: inline;'); // this 
$("#result").attr('Style','display: block;'); // or this

}); 

For Hide 隐藏

$("#lnk").click(function () {  
$("#result").removeAttr('Style');
$("#result").attr('Style','display: none;');
}); 

Here Are Some More Codez 这里有更多的Codez

 <a href="#" id="yourlink">yourlink</a>
    <form id="yourform" style="display:none;">form here.</form>
    <script>
    $(document).ready(function () {
        $('#yourlink').click(function () {
            $('#yourform').css('display', 'inline');
        });
    });
    </script>

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

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