laravel 如何使用 jquery 为 data-url 属性设置新值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34369276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:50:14  来源:igfitidea点击:

How to set a new value for data-url attribute using jquery?

javascriptjqueryajaxlaraveljquery-ajaxq

提问by Himanshu Dwivedi

Before page load:

页面加载前:

<a data-url="htts://xyz@..." class="mybuttons"  data-type="facebook">

After: On page load using jquery I used to append new data-url

之后:在使用 jquery 的页面加载时,我曾经附加新的数据 url

<a data-url="https://abc@..." class="mybuttons"  data-type="facebook">

Now when I want to share this new data url using facebook api then it show old url https://xyz@...in url bar.

现在,当我想使用 facebook api 共享这个新的数据 url 时,它会https://xyz@...在 url 栏中显示旧的 url 。

I want new data-urlto be shared.

我想data-url分享新的。

I have successfully change the data-urlusing $(this).attr('data-url',value.url). What to do please help me out. Even my new data-urlis not getting show in page source.

我已成功更改data-urlusing $(this).attr('data-url',value.url). 该怎么办,请帮帮我。甚至我的新内容data-url也没有在页面源代码中显示。

回答by Moppo

In your line:

在您的行中:

$(this).attr('data-url',value.url);

are you sure thisrefers to the right DOM object?

你确定this指的是正确的 DOM 对象吗?

Try adding an id to the anchor:

尝试将 id 添加到锚点:

<a id='face-url' data-url="htts://xyz@..." class="mybuttons"  data-type="facebook">

Then in your code, set the data-urlattribute this way:

然后在您的代码中,以data-url这种方式设置属性:

$('#face-url').data('url', value.url);

Now that you have set the attribute value, you have to be surethat the facebook API reads this attribute only after you've changed it

现在您已经设置了属性值,您必须确保facebook API 仅在您更改该属性后才读取该属性

回答by Fabio Costa

I know it's too late but I had this problem and I think that I have the solution For me the solution was change the way to set data.

我知道为时已晚,但我遇到了这个问题,我认为我有解决方案对我来说,解决方案是改变设置数据的方式。

$(this).data('url', value.url);