将参数从 onclick 事件传递到使用数据对象构建 HTML 的函数中 - 到目前为止在 jsfiddle 中工作了一半

发布于 2024-12-10 12:48:15 字数 2204 浏览 1 评论 0原文

我正在尝试从 javascript 对象访问数据,以便使用适当的信息动态填充 div。在我的示例中,教程分为三个步骤。当用户完成每个步骤后,他们单击按钮继续下一步。我希望将函数“getStepData(stepNumber)”称为 onclick,并将步骤号传递到函数中并在我在 javascript 中构建的 html 中使用。示例代码在这里,但我也有一半在 jsFiddle 中工作:

http://jsfiddle.net/ enajenkins/xvFeX/24/

//create data object that will hold all data to be accessed later.               
var tutorialDataObj = {    
//define the tutorial container
profilePageTutorials: {        
    //define the steps and their data
    step1: {
        id: "tip1",
        class: "profile_tip",
        title: "Step 1",
        subtitle: "How to edit your profile"
    },//END: step1        
    step2: {
        id: "tip2",
        class: "search_tip",
        title: "Step 2",
        subtitle: "How to search"
    },//END: step2        
    step3: {
        id: "tip3",
        class: "change-photo_tip",
        title: "Step 3",
        subtitle: "How to upload a new photo"
    }//END: step3                
}//END: profileTutorial    
}//END: tutorialDataObj
//identify the div container that the dynamic html will be written into
var tutorialWindow = document.getElementById("tutorial-step-window");
//shorten the path to the data in the object
var tutorials = tutorialDataObj.profilePageTutorials;
//loop through each step in the data object
for ( var step in tutorials ) {
    //build the html to be inserted dynamically using data from object
    var html = ["<h2 id='title'>" + tutorials.step1.title + "</h2>" +
                "<h3>" + tutorials.step1.subtitle + "</h3>"];
    //insert the html into the div container
    tutorialWindow.innerHTML = html;
    alert(tutorials[step].subtitle);
}

function getStepData (stepNumber) {
alert(stepNumber); //here, I'm trying to access the argument from the input button.     I'd like to pass it into the html I'm building above so I can access the data from the intended step when the user clicks the button.
}

这是 html:

<div id="tutorial-step-window" class="tip">HOWDY</div>
<input type="button" value="Change Data" onclick="getStepData(3)" />

I am trying to access data from a javascript object in order to dynamically populate a div with the appropriate info. In my example, there are three steps in a tutorial. When the user is finished with each step, they click the button to go on to the next step. I'd like the function "getStepData(stepNumber)" to be called onclick, and for the step number to be passed into the function and used in the html I am building in the javascript. The sample code is here, but I also have it half working in jsFiddle:

http://jsfiddle.net/enajenkins/xvFeX/24/

//create data object that will hold all data to be accessed later.               
var tutorialDataObj = {    
//define the tutorial container
profilePageTutorials: {        
    //define the steps and their data
    step1: {
        id: "tip1",
        class: "profile_tip",
        title: "Step 1",
        subtitle: "How to edit your profile"
    },//END: step1        
    step2: {
        id: "tip2",
        class: "search_tip",
        title: "Step 2",
        subtitle: "How to search"
    },//END: step2        
    step3: {
        id: "tip3",
        class: "change-photo_tip",
        title: "Step 3",
        subtitle: "How to upload a new photo"
    }//END: step3                
}//END: profileTutorial    
}//END: tutorialDataObj
//identify the div container that the dynamic html will be written into
var tutorialWindow = document.getElementById("tutorial-step-window");
//shorten the path to the data in the object
var tutorials = tutorialDataObj.profilePageTutorials;
//loop through each step in the data object
for ( var step in tutorials ) {
    //build the html to be inserted dynamically using data from object
    var html = ["<h2 id='title'>" + tutorials.step1.title + "</h2>" +
                "<h3>" + tutorials.step1.subtitle + "</h3>"];
    //insert the html into the div container
    tutorialWindow.innerHTML = html;
    alert(tutorials[step].subtitle);
}

function getStepData (stepNumber) {
alert(stepNumber); //here, I'm trying to access the argument from the input button.     I'd like to pass it into the html I'm building above so I can access the data from the intended step when the user clicks the button.
}

and here is the html:

<div id="tutorial-step-window" class="tip">HOWDY</div>
<input type="button" value="Change Data" onclick="getStepData(3)" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-12-17 12:48:15

试试这个..

http://jsfiddle.net/xvFeX/45/

我稍微做了一下与您描述的方式不同,但实现相同的目标:

我删除了按钮上的内联单击事件并添加了一个 id。
然后我在js中添加了点击,并根据对象中的步骤数进行了计数。

Try this..

http://jsfiddle.net/xvFeX/45/

I go about it a slightly different way then you describe but accomplish the same goal:

I remove the inline click event on the button and added an id.
then I added the click in the js and also did a count in there based on the number of steps in the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文