[英]How to add/alter a value of an attribute
我有一張與html的“ Area Map
一起使用的圖片
我在頁面上添加了一些引導程序,但總的來說很短。
我想在加載后根據圖片的位置動態地更改我所在區域的坐標。
我得到這樣的圖片位置
var addLEFT = $("#body_hand_foot_image").position().left;
var addTOP = $("#body_hand_foot_image").position().top;
所以從這里我想將兩個變量的值添加到區域的坐標中。 以下是這些領域之一的示例。 它們都是精確的,圖片位於x = 0-y = 0。
<area class="joint" alt="Front Right Neck" href="#" joint="R_Neck_front" full="Right Neck" shape="circle" coords="126,92,8" />
有任何想法嗎?
尋求幫助后的最終解決方案
function reconfCoords() {
var items = $('#jointMap').find('area');
items.each(function () {
var c = $(this).attr('coords');
var coords = c.split(',');
coords[0] = (Number(coords[0]) + Number($("#body_hand_foot_image").position().left)).toString();
coords[1] = (Number(coords[1]) + Number($("#body_hand_foot_image").position().top)).toString();
$(this).attr('coords',coords.join());
var a = $(this).attr('coords');
});
return true;
}
嘗試做這樣的事情:
var coords = $('area').attr('coords').split(',');
coords[0] = $("#body_hand_foot_image").position().left;
coords[1] = $("#body_hand_foot_image").position().top;
$('area').attr('coords',coords.join());
在jQuery中使用$ elem.attr()來獲取和設置元素屬性。 在原始HTML中使用Element.getAttribute()和Element.setAttribute()。
我假設您要獲取“ coords”屬性,在“,”上拆分值,分別將值添加到[0]和[1],通過重新加入數組來重新創建coord值,然后設置屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.