Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JS更改中元素的?_Javascript_Html_Replace_Href - Fatal编程技术网

Javascript 如何使用JS更改中元素的?

Javascript 如何使用JS更改中元素的?,javascript,html,replace,href,Javascript,Html,Replace,Href,请帮我解决这个问题。 我是JS的新手,所以我不知道如何做:我需要替换以下字符串中的参数 <a href="javascript:PhotoUploadDialog.show(false, 'crop_moderation_split');" class="profile-gallery__item__link"></a> 由于元素具有类profile-gallery\uu项,因此可以执行此操作 let anchor = document.getElementsByCla

请帮我解决这个问题。 我是JS的新手,所以我不知道如何做:我需要替换以下字符串中的参数

<a href="javascript:PhotoUploadDialog.show(false, 'crop_moderation_split');" class="profile-gallery__item__link"></a>
由于元素具有类profile-gallery\uu项,因此可以执行此操作

let anchor = document.getElementsByClassName("profile-gallery__item a")
anchor.setAttribute("href", "javascript:PhotoUploadDialog.show(false, 'crop_split');")

只需选择锚元素并更改属性

const anchor = document.querySelector('.profile-gallery__item__link');
anchor.setAttribute('href', 'javascript:PhotoUploadDialog.show(false, 'crop_split');')
如果需要更改多个链接的属性,可以将它们全部选中,并在循环中执行此操作,如下所示:

const anchorAll = document.querySelectorAll('.profile-gallery__item__link');
for (let i = 0; i < anchorAll.lenght; i++) {
  anchorAll[i].setAttribute('href', "javascript:PhotoUploadDialog.show(false, 'crop_split');")}

例如,为了尽可能接近您目前拥有的代码,您可以向标记添加一个id,以便更容易地识别它

<a id="myLink" href="javascript:PhotoUploadDialog.show(false, 'crop_moderation_split');" class="profile-gallery__item__link"></a>

<script>
function changeLink() {
    var link = document.getElementById("myLink");
    link.setAttribute('href', "javascript:PhotoUploadDialog.show(false, 'crop_split');");
}
</script>
这里要做的是,首先存储一个名为whichAction的变量,并将其设置为1。当您单击链接时,将调用一个名为doAction的函数,该函数查看whichAction的值并执行适当的任务。当然,如果将来需要更多的选择,您可以在这里添加任意数量的案例

现在,changeLink函数需要做的就是更改whichAction变量的值


还要注意的是,在doAction函数的末尾,必须返回false;,这会告诉浏览器不要继续跟随href=属性中的链接。

显示您尝试过的内容。已经尝试了完全相同的代码。不幸的是,它不起作用。上面的变量列表只引用了类名为profile-gallery\uu项的第一个列表元素。到底是什么不起作用?您是否检查了页面以查看属性是否已设置?是的,我当然检查了页面。我每次运行代码时都这样做。不会发生任何情况,href值不会更改。页面上有两个元素,我上面提到的是第一个。无论我尝试运行的JS代码的变体是什么,它们都不会更改。您希望修改的href元素是什么?不幸的是,我无法更改HTML代码中的任何内容,因为该页面不是我的,它来自Internet上的现有站点。我正在尝试使用时尚的插件来改变它。在问题中提到这一点可能是个好主意,它非常相关。是的,您在JSFIDLE上编写的代码工作得很好,但在实际页面上却不行。什么也没有发生,href的值没有改变。我还可以尝试什么?请检查您的代码放在哪里。把它放在关闭标签之前。还要检查控制台是否有错误。
const anchor = document.querySelector('.profile-gallery__item__link');
anchor.setAttribute('href', 'javascript:PhotoUploadDialog.show(false, 'crop_split');')
const anchorAll = document.querySelectorAll('.profile-gallery__item__link');
for (let i = 0; i < anchorAll.lenght; i++) {
  anchorAll[i].setAttribute('href', "javascript:PhotoUploadDialog.show(false, 'crop_split');")}
<a id="myLink" href="javascript:PhotoUploadDialog.show(false, 'crop_moderation_split');" class="profile-gallery__item__link"></a>

<script>
function changeLink() {
    var link = document.getElementById("myLink");
    link.setAttribute('href', "javascript:PhotoUploadDialog.show(false, 'crop_split');");
}
</script>
<a href="#" onClick="doAction()" class="profile-gallery__item__link"></a>

<script>
var whichAction = 1;

function doAction() {
    switch (whichAction) {
        case 1:
        PhotoUploadDialog.show(false, 'crop_moderation_split');
        break;

        case 2:
        PhotoUploadDialog.show(false, 'crop_split');
        break;
    }
    return false;
}

function changeLink() {
    whichAction = 2;
}
</script>