简体   繁体   中英

Get Javascript Params and Write them

I am a little desperate and I need the help of a generous person here!

I have been hired to do a difficult project, but I am only a PHP developer and I know very few about Javascript.

What I need to do is to create a function in Javascript that will be called by an external script ( I do not have this script, so I know only the params the script will pass to the function ). When the function has been called and the params have been passed to the function, this function will start writing some HTML, inside the blocks that the other script has already cloned.

So, the other script will clone the HTML blocks all the times that is necessary and will assign an ID in the object of the cloned HTML block. At this point my function, receives the ID of the cloned HTML block and will start writing inside the cloned block.

This is not clear for me.

This is the example of the code:

function showSubject(params) {
    var targetElement = params.targetElement;
    var subjectName = params.subjectName;
    var numMentions = params.numMentions;
    var numPositive = params.numPositive;
    var numNegative = params.numNegative;
    var imageSrc = params.imageSrc;
    /* write the code in the HTML block */
    return (true or false) //true if all is fine
};

Example of the params my function will get.

var params = {
    candidatesObject: "cloned item",
    candidateName: "Nome Cognome",
    numMentions: 1000,
    numPositive: 1000,
    numInformative: 1000,
    numNegativers: 1000 };

Can anyone give me a direction? An example? Something please... :(

The easiest way I can think of is:

function showSubject(params) {
    //var targetElement = ...
    //don't bother with all that

    /* write the code in the HTML block */
    var clonedHTML = document.getElementByID( params.targetElement );
    if( !clonedHTML ){
        return false;
    }

    var iDontKnowWhatYouNeedToWrite = "<span style='background:yellow'>"
        + 'subjectName = ' + params.subjectName + "<br />"
        + 'numMentions = ' + params.numMentions + "<br />"
        + 'numPositive = ' + params.numPositive + "<br />"
        + 'numNegative = ' + params.numNegative + "<br />"
        + 'imageSrc = ' + params.imageSrc
        + '</span>';

    clonedHTML.innerHTML = iDontKnowWhatYouNeedToWrite;
    return true;
};

Obviously iDontKnowWhatYouNeedToWrite is whatever you need it to be. Also, wouldn't bother copying the params out into individual variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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