重置jquery函数并清除变量

时间:2018-02-11 02:46:19

标签: javascript jquery

我有一个动态网站,点击一个元素会生成一个新元素(% create a custome color map map = zeros(11,3); reds = 0:0.1:1; map(:,1) = reds(:); colormap(map); figure(1) depth = [1 2 3 4]; num_depth = length(depth); for index = 1: num_depth subplot(num_depth,1,index); step = 1/(2^depth(index)-1); A = 0:step:1; imagesc(0:1,0:1,A) axis([0 1 0 1]) set(gca,'ytick',[]) end )并指定两个匹配div属性的元素。我想修改新的data-id,因此我有一个小脚本,可以从匹配的div中找到新的div

这一切都很有效:

data-id

但是,新的var id = 123; //this represents the randomly generated data-id $('.modules').click(function() { var addOne = $(this); //**this is just to simulate what the live site does: appending the clicked element with 'data-id="[the random id]"** $(addOne).attr('data-id', id); //put the data-id value from the click element in a variable var addedId = addOne.data('id'); //find the newly created DOM element with the matching data-id value and make it a variable var module = $('.module-added[data-id="' + addedId + '"]')[0]; //check and make sure I got the element so I can mess with it. var x = module.getAttribute("data-id"); alert(x); // element.scrollIntoView(); //like maybe this }); 可以删除,div会从点击元素中删除,然后点击该元素可以再次点击 new {{1} } - 都有新的data-id s。

重新点击click元素时出现问题。我的脚本仍然保留原始的div值,而不是拿起新分配的值。

我很确定这很简单,但我是个新手。

以下是Codepen供参考

0 个答案:

没有答案
相关问题