简体   繁体   English

需要帮助来制作带有js的按钮,然后将其链接到函数和CSS代码

[英]Need help making button with js then linking it to a function and css code

I need am essentially making a piece of code that will inject my own code onto the webpage. 实际上,我需要编写一段代码,将自己的代码注入到网页中。

Due to me being limited on my ipad I am using bookmarklets so here it is: 由于我在ipad上的使用受到限制,因此我在使用书签,因此它是:

I am using this code to call on a js file that will create the button 我正在使用此代码调用将创建按钮的js文件

    javascript: (function () { 
        var jsCode = document.createElement('script'); 
        jsCode.setAttribute('src', 'http://path/to/external/file.js');                  
        document.body.appendChild(jsCode); 
     }());

The button I want to make will need to be created with the code shown below: 我要创建的按钮将需要使用以下代码创建:

    <button id=“supermoneybutton” onclick=“moneyget()”>get money</button>

Then I need to add a link to an external css file. 然后,我需要添加一个指向外部CSS文件的链接。

I have the file for the css, I just need help making the js work, the injection code already works it just shows my method for injection. 我有css的文件,我只需要帮助使js正常工作,注入代码已经起作用,它只是显示了我的注入方法。

In your JS code that dynamically creates the button, you should add the event handler there too: 在动态创建按钮的JS代码中,您也应该在其中添加事件处理程序:

(function () {
    var button = document.createElement('button');
    button.id = 'supermoneybutton';
    button.addEventListener('click', function (e) {
        // Do Stuff
        moneyget();
    });
    document.body.appendChild(button);

    // Add CSS File
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.href = '/path/to/cssfile';
    link.type = 'text/css';
    var linkRef = document.getElementsByTagName("link")[0];
    linkRef.parentNode.insertBefore(link, linkRef);
})();

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

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