- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
不确定如何在标题中准确描述我的问题,但是......
我想要做的是将菜单项的 ID 或文本推送到输入字段 <input id="assignment"/>
当它被点击时。每次页面加载从此 spreadsheet 提取的数据时,都会动态填充菜单。 。菜单项<a></a>
在初始化以下“发送脚本”后加载。
// Send to Input Script //
$('#byAssignment a').click(function() {
var id = $(this).text();
var input = $('#assignment');
input.val(id);});
// End Send to Input Script //
显然这是行不通的。我该如何解决这个问题?任何帮助将不胜感激。谢谢。
完整代码如下:
// Google Visualization Query Script //
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(querymenu);
function querymenu() {
var queryMENU = new google.visualization.Query('https://docs.google.com/spreadsheet/ccc?key=1HpHMfoEnPgESb2XPVCgb7XyGwRAvrq3EoQj4WHj4vhA&sheet=QUERY2');
queryMENU.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in Query: Please Refresh Page ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var datatable = response.getDataTable();
for (var i = 1; i < datatable.getNumberOfColumns(); i++) {
var ddItemValue = datatable.getValue(0, i);
var ddMenuItem = document.getElementById('byAssignment').appendChild(document.createElement('A'));
ddMenuItem.href = '#';
ddMenuItem.id = ddItemValue;
ddMenuItem.innerHTML = ddItemValue;
}
}
// END Google Visualization Query Script //
// Menu Script //
function AssignmentMenu() {
document.getElementById('byAssignment').classList.toggle('show');
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName('dropdown-content');
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
// END Menu Script //
// Send to Input Script //
$('#byAssignment a').click(function() {
var id = $(this).text();
var input = $('#assignment');
input.val(id);
});
// END Send to Input Script //
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #f1f1f1}
.show {display:block;}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
cursor: pointer;
}
.dropdown a:hover {background-color: #f1f1f1}
.show {display:block;}
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="dropdown">
<button onclick="AssignmentMenu()" class="dropbtn">Assignment</button>
<div id="byAssignment" class="dropdown-content">
<a>this works</a>
</div>
</div>
<input id="assignment"/>
最佳答案
创建项目时添加事件处理程序...
var ddMenuItem = document.getElementById('byAssignment').appendChild(document.createElement('A'));
ddMenuItem.href = '#';
ddMenuItem.id = ddItemValue;
ddMenuItem.innerHTML = ddItemValue;
// add event listener
$(ddMenuItem).click(function() {
var input = $('#assignment');
input.val($(this).text());
});
请参阅以下工作片段...
// Google Visualization Query Script //
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(querymenu);
function querymenu() {
var queryMENU = new google.visualization.Query('https://docs.google.com/spreadsheet/ccc?key=1HpHMfoEnPgESb2XPVCgb7XyGwRAvrq3EoQj4WHj4vhA&sheet=QUERY2');
queryMENU.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in Query: Please Refresh Page ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var datatable = response.getDataTable();
for (var i = 1; i < datatable.getNumberOfColumns(); i++) {
var ddItemValue = datatable.getValue(0, i);
var ddMenuItem = document.getElementById('byAssignment').appendChild(document.createElement('A'));
ddMenuItem.href = '#';
ddMenuItem.id = ddItemValue;
ddMenuItem.innerHTML = ddItemValue;
// add event listener
$(ddMenuItem).click(function() {
var input = $('#assignment');
input.val($(this).text());
});
}
}
// END Google Visualization Query Script //
// Menu Script //
function AssignmentMenu() {
document.getElementById('byAssignment').classList.toggle('show');
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName('dropdown-content');
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
// END Menu Script //
// END Send to Input Script //
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #f1f1f1}
.show {display:block;}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
cursor: pointer;
}
.dropdown a:hover {background-color: #f1f1f1}
.show {display:block;}
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="dropdown">
<button onclick="AssignmentMenu()" class="dropbtn">Assignment</button>
<div id="byAssignment" class="dropdown-content">
<a>this works</a>
</div>
</div>
<input id="assignment"/>
关于javascript - 使用 Javascript 将文本(从 Google 可视化查询)发送到点击时的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676618/
menu Home Events Technical Schedule
我试图阻止触发默认 anchor 链接和 onclick 事件。 这是 HTML 代码: Google 我正在尝试使用以下 jQuery 代码阻止任何重定向的发生: $("#mylink").cli
我想在单击父 div 上的任意位置时切换我的 div,除非单击 anchor 元素。因此,例如,如果我单击示例中的第一个文本,我希望它切换,但在我的示例中的第二个文本上,我不希望它切换。 JSFidd
我在通过 jQuery 伪造 anchor 点击时遇到问题:为什么我的thickbox在我第一次点击输入按钮时出现,但第二次或第三次却没有出现? 这是我的代码: Link 当我直接点击链接时,它总是
我已经从 Mootools 切换到 jQuery,因为我认为它有更好的支持。我有这样的 HTML: Opcje Opcje Opcje Opcje Opcje Opcje Opcje JS
我是一名优秀的程序员,十分优秀!