如何在Laravel Blade框架中实现Intro.js按钮引导功能?
Hey there! Let's get your Intro.js working properly in Laravel Blade. The issue with your current jQuery code is that you're trying to use href() and onclick() as if they're jQuery methods—those are actually HTML attributes, not functions you can call inside a click handler. Here's how to fix it:
Step 1: Correct Your Button HTML
First, make sure your button (link) has the correct id and a proper href (we'll handle preventing the default action in the script):
<a class="btn btn-large btn-success" href="#" id="belajarr">Show me how</a>
Step 2: Fix the jQuery Click Handler
Replace your existing script with this. We'll stop the link from reloading the page with e.preventDefault() and directly call the Intro.js method, just like your original onclick did:
$(document).ready(function(){ $('#belajarr').on('click', function(e){ // Prevent the link from navigating to a new page e.preventDefault(); // Initialize Intro.js with your desired option introJs().setOption('showProgress', true).start(); }); });
Key Things to Double-Check
- Intro.js is Loaded: Make sure you've included the Intro.js script and CSS in your Blade template. If you're using Laravel Mix, you might have imported it in
resources/js/app.jslikeimport introJs from 'intro.js';and imported the CSS inresources/css/app.csswith@import 'intro.js/introjs.css';. Alternatively, you can use CDN links in your layout blade file (just make sure they load after jQuery if you're using jQuery). - jQuery is Available: Ensure jQuery is loaded before your custom script. In newer Laravel versions, jQuery isn't included by default, so you might need to install it via npm (
npm install jquery) and import it in your app.js, or add a CDN link for jQuery.
That should do it! When you click the button now, the Intro.js tour should start just like it did with the original inline onclick.
内容的提问来源于stack exchange,提问作者Ryan Ariel Winata

