繁体   English   中英

HTML / JS-从index.js onclick调用函数

[英]HTML / JS - Calling a function from index.js onclick

我想从我的functional.js中的index.js内调用一个函数,但是因为它链接到浏览器,所以我不能使用import或require?

我有一个index.js,index.html和另一个functional.js文件。

function.js文件由index.html使用,在此文件中,我会在特定情况下附加一些html,而我试图做的是onclick调用一个函数,但是从我的index.js文件中调用了此函数,在那里,因为它需要一个socket.id。

看这里:

* functionality.js:

$('#messages').append($('<li id="messageclient">').append($(`
        <div id="message-cont" class="message-cont">     
        <div class="orderDetailsWrapper">
        <div class="detailsHeaderWrapper">
            <div class="orderNum"></div>
            <div class="customerName"></div>
        </div>

        <div class="textToCustomer">
        <p> Please click on the item you want to return</p>

       </div>

        <div class="itemBoxWrapper">
            <div class="item1Wrapper" onclick="matchingResponse()">
                <div class="item1Title"></div>
                <div class="item1Price"></div>
            </div>

* index.js

function matchingResponse() {
    var options = {
        method: 'POST',
        uri: 'https://dialogflow.googleapis.com/v2beta1/projects/returnsbot-50668/agent/sessions/1:detectIntent',
        headers: {
            'Authorization': `Bearer ${oAuthToken}`,
            'Content-Type': 'application/json'
        },
        body: {
            "queryInput": {
                "event": {
                    "name": "Matching",
                    "languageCode": "en"
                }
            }
        },
        json: true // Automatically stringifies the body to JSON
    };
    // initial message triggering.
    rp(options)
        .then(function (parsedBody) {
            console.log('DF response' + JSON.stringify(parsedBody))
            console.log('text ^^^^^^^^^^^^^^ ' + JSON.stringify(parsedBody.queryResul))
            text = parsedBody.queryResult.fulfillmentText;

            response = text;
            console.log('matching response +++++++++++' + text);

            data = [details.chatuser, response]
            io.of('/main').to(socket.id).emit('response', data);

        })
        .catch(function (err) {
            console.log(err);
        });
}

因为套接字函数在index.js文件中,所以我需要在其中包含这个matchresponse函数以使用适当的ID。

问题是在functional.js中,我无法使用导入/要求。

我能做什么?

如果您未在加载时调用任何函数,则只需向HTML中添加functional.js和index.js文件即可。

暂无
暂无

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

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