2011-02-17 16 views
34

Just wondering if there is a way to get a HTML <button> element to link to a location without wrapping it in an <a href... tag?वहाँ एक रास्ता एक <a href ... tag?

Button currently looks like:

<button>Visit Page Now</button> 

What I would prefer not to have:

<a href="link.html"><button>Visit Page Now</button></a> 

The button is not being used within a form so <input type="button"> is not an option. I am just curious to see if there is a way to link this particular element without needing to wrap it in an <a href tag.

Looking forward to hearing some options/opinions.

+2

आप एक बटन क्यों चाहते हैं एक लिंक होना चाहते हैं? सिर्फ एक लिंक क्यों नहीं है? – robertc

+0

@robertc, मैं आमतौर पर एक लिंक का उपयोग करता हूं लेकिन फिर

+5

बटन तत्व का बिंदु एक स्क्रिप्ट फ़ंक्शन को ट्रिगर करना है, जैसे किसी फॉर्म पर डिलीवरी लागत को पुन: गणना करना या चयनित टेक्स्ट में बोल्ड स्वरूपण लागू करना, जो HTML द्वारा मूल रूप से समर्थित नहीं है। पृष्ठों के बीच नेविगेट करना कुछ ऐसा है जो HTML द्वारा समर्थित है, आपको ऐसा करने के लिए स्क्रिप्ट की आवश्यकता नहीं है, इसलिए आपको एक बटन की आवश्यकता नहीं है। – robertc

उत्तर

54

Inline Javascript:

<button onclick="window.location='http://www.example.com';">Visit Page Now</button> 

Defining a function in Javascript:

<script> 
    function visitPage(){ 
     window.location='http://www.example.com'; 
    } 
</script> 
<button onclick="visitPage();">Visit Page Now</button> 

or in Jquery

<button id="some_id">Visit Page Now</button> 

$('#some_id').click(function() { 
    window.location='http://www.example.com'; 
}); 
+4

धन्यवाद @aiham, यह काम किया। इसे अधिक लचीला बनाने के लिए, यूआरएल विज़िट पेज फ़ंक्शन के लिए एक तर्क होना चाहिए। – Dan

+0

मैं उपयोग कर रहा हूँ ** ASP.NET MVC **। मैं डेटा को 'पोस्ट' कैसे कर सकता हूं और ** नियंत्रक ** ** अन्य ** कार्य विधि ** पर सीधे कैसे कर सकता हूं? – barnes

+0

इससे एक लिंक तत्व वांछित तत्व होगा के रूप में एक भ्रामक जवाब है। तत्व चुनते समय अलग कार्यक्षमता और डिजाइन! – roka

4

You can make it a non-submitting button (<button type="button">) and hook something like window.location = 'http://where.you.want/to/go' into its onclick handler. This does not work without javascript enabled though.

Or you can make it a submit button, and do a redirect on the server, although this obviously requires some kind of server-side logic, but the upside is that is doesn't require javascript.

(actually, forget the second solution - if you can't use a form, the submit button is out)

6

Just do this

<button OnClick=" location.href='link.html' ">Visit Page Now</button> 

Although, it's been a while since I've touched JavaScript - maybe location.href is outdated? Anyways, that's how I would do it.

5

Well, for a link, there must be a link tag around. what you can also do is that make a css class for the button and assign that class to the link tag. like,

THE CSS

#btn{ background:url(images/button.jpg) no-repeat 0 0; display:block; width:50px; height:20px; border:none; outline:none;} 

THE HTML

<a href="btnlink.html" id="btn"></a> 
22

यहाँ में यह लपेटकर के बिना एक स्थान से जोड़ने के लिए एक

2

<form action="portfolio.html"> <button type="link" class="btn btn-primary btn-lg">View Work</button> </form>

मैं सिर्फ यह पता लगा है, और यह मेरा डिफ़ॉल्ट बिना एक पृष्ठ पर पूरी तरह से लिंक मेरे बटन कक्षाओं सवारी पर लिंक सेटिंग्स! :)

2

बस एक साइड नोट - आप < a href> डिफ़ॉल्ट रूप से जानता है कि चालें पर विचार करना चाहेंगे लेकिन जावास्क्रिप्ट लिंकिंग आपके लिए नहीं करेगी। अर्थात्:

  • यदि आप Ctrl के साथ क्लिक करते हैं, तो एक नया टैब खोला जाएगा;
  • यदि आप Shift के साथ क्लिक करते हैं, तो ब्राउज़र एक नई विंडो में लिंक खोलता है;
  • यदि आप Alt के साथ क्लिक करते हैं, तो यह "डाउनलोड लक्ष्य" कमांड है।

अब आप कि सभी व्यवहार अनुकरण करने के लिए नहीं करना चाहते हैं, मैं <, यह एक बटन की तरह एक href> और शैली का उपयोग करने के बाद से बटन ही मोटे तौर पर एक आकार और एक हॉवर प्रभाव है सुझाव देते हैं। मुझे लगता है कि अगर यह केवल "बटन और कुछ और नहीं" के लिए अर्थपूर्ण रूप से महत्वपूर्ण नहीं है, < a href> समुराई का मार्ग है। और यदि आप अर्थशास्त्र और पठनीयता के बारे में चिंता करते हैं, तो जब आपका दस्तावेज़ तैयार होता है तो आप बटन तत्व को भी प्रतिस्थापित कर सकते हैं()। यह स्पष्ट और सुरक्षित है।

संबंधित मुद्दे