将onclick事件的参数传递到使用数据对象构建HTML的函数中——到目前为止在js中只完成了一半
Pass parameter from onclick event into function that builds HTML using data object - half working so far in jsfiddle
我试图从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)" />
试试这个…
http://jsfiddle.net/xvFeX/45/我做这件事的方式和你描述的略有不同,但实现了同样的目标:
我删除了按钮上的内联click事件并添加了id。然后我在js中添加了click,并根据对象的步数进行了计数。
相关文章:
- 如何将变量传递到另一个js文件
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- 将回车键映射到HTML/CSS/JS中的按钮
- 使用Express4将数组从Nodejs中的app.js传递到routes/index.js
- AJAX将JSON数组保存到多个JS变量中
- 如何将会话数据从集合传递到视图?(Backbone JS/Coffeescapept)
- 将表单对象保存到具有主干.js的模型
- 找不到在 create-js 中运行的动画,未列出任何错误
- 如何将多个 Blender JSON 模型合并到一个 Three.js Object3D 对象中
- 如何将async.whilst调用的结果发送到和express.js res.send()
- 可以'我找不到关于koa.js研讨会解决方案的解释,练习一
- R直方图到动态HTML/JS
- 如何将文本区域的内容保存到PHP或JS中的html文件中
- 应用<strong>标记到我的js.coffee文件中的字符串
- 如何将新的动态内容添加到我的JS代码中
- 如何分析本地存储项到目前为止
- $injector:modulerr AngularJS,到目前为止已经尝试了所有内容
- 我想在点击id="add_city"按钮时激活该功能.到目前为止,它在按Enter键时激活
- 将onclick事件的参数传递到使用数据对象构建HTML的函数中——到目前为止在js中只完成了一半
- 给定到目前为止的字符串格式