[英]How to get selected image map alt value?
ok changed my code: 确定更改了我的代码:
<div class="a" id="mySidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">X</a>
<a href="#" id="tname">Number:</a>
<a href="#" id="d1"></a>
<a href="#" id="d2"></a>
</div>
<map class="b" name="mymap" id="map">
<area shape="circle" coords="67,420,20" onclick="openNav(this)" alt="5" />
<area shape="circle" coords="184,384,19" onclick="openNav(this)" alt="6" />
</map>
function openNav(element){
$('#mySidenav').css("width", "250px");
$('#image').css("marginLeft", "250px");
var alt = $(element).attr("alt");
$('#tname').text(alt);
}
i have folders named "5" and "6" and so on. 我有名为“ 5”和“ 6”的文件夹,依此类推。 each folders have
1.html 2.html
that has <title>PersonName</title>
if i clicked image map with alt=5 i want to get '5/1.html' <title>
value appear on "id=d1"
and it should become link to that html. 如果我单击alt = 5的图像映射,则每个文件夹的
1.html 2.html
都具有<title>PersonName</title>
,我想获取'5 / 1.html' <title>
值,该值显示在"id=d1"
它应该成为该html的链接。 tried this one, but not working, seems like im making syntax error 尝试了这个,但不起作用,似乎即时通讯使语法错误
$.get('1.html'), openNav(result){
var obj = $(result).find('title');
$(this).append($('#d1').text(obj.html()));
You can select the attribute with plain JavaScript with .getAttribute("alt")
or with jQuery with .attr("alt")
. 您可以使用带有
.getAttribute("alt")
普通JavaScript或带有.attr("alt")
jQuery选择属性。 However, you need to select the correct element first. 但是,您需要首先选择正确的元素。 You can do this eg with jQuery with
$("#map area")
. 您可以使用带有
$("#map area")
jQuery完成此操作。 It is however easier to pass the element directly by in the function. 但是,在函数中直接传递元素会更容易。
HTML: HTML:
<map class="b" name="mymap" id="map">
<area shape="circle" coords="67,420,20" onclick="openNav(this)" alt="5" />
<area shape="circle" coords="184,384,19" onclick="openNav(this)" alt="6" />
</map>
JavaScript: JavaScript:
function openNav(element) {
var alt = element.getAttribute("alt");
document.getElementById("tname").innerHTML = alt;
}
// or with jQuery
function openNav(element) {
var alt = $(element).attr("alt");
$("#tname").text(alt);
}
The answer for the question after your edit: 编辑后问题的答案:
The proper solution would be to set a click listener on the area. 正确的解决方案是在该区域设置点击监听器。 Inside of this clicklistener, you create the whole logic, like this.
在此Clicklistener内,您可以像这样创建整个逻辑。
First, remove every onclick=""
from your HTML document. 首先,从HTML文档中删除所有
onclick=""
。
Then, use this JavaScript snippet: 然后,使用以下JavaScript代码段:
$("#map area").click(function() {
var alt = $(this).attr("alt");
var address1 = alt + "/1.html";
var address2 = alt + "/2.html";
$.get({
url: address1,
success: function(result) {
var title = $.parseHTML(result).find("title").text();
$("#d1").text(title).attr("href", address1);
}
});
$.get({
url: address2,
success: function(result) {
var title = $.parseHTML(result).find("title").text();
$("#d2").text(title).attr("href", address2);
}
});
});
There is of course room for improvements (eg DRY concept etc.). 当然还有改进的余地(例如DRY概念等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.