Javascript 如何将cookie附加到没有id的iFrame中的src的末尾?

Javascript 如何将cookie附加到没有id的iFrame中的src的末尾?,javascript,jquery,html,iframe,cookies,Javascript,Jquery,Html,Iframe,Cookies,所以我写了一些cookie,但是我需要在iframe中将信息附加到src的末尾。问题是我不知道如何选择iframe,因为它没有id iframe如下所示: <iframe src="www.mysite.com";> 我需要它看起来像: <iframe src="www.mysite.com&cookie";> 但是,我不能使用id来选择iframe,因为我不是创建iframe的人,如果其他页面上也有iframe,则需要以相同的方式选择和更新它们。因此,该功能

所以我写了一些cookie,但是我需要在iframe中将信息附加到src的末尾。问题是我不知道如何选择iframe,因为它没有id

iframe如下所示:

<iframe src="www.mysite.com";>
我需要它看起来像:

<iframe src="www.mysite.com&cookie";>
但是,我不能使用id来选择iframe,因为我不是创建iframe的人,如果其他页面上也有iframe,则需要以相同的方式选择和更新它们。因此,该功能需要具有通用性。如果你想看我的cookies代码,我也可以给你看

如果这是太模糊,让我知道,我会更新它

以下是我认为我正在努力做的一些事情


您需要在每个iframe src中附加一些内容吗

var iframes = document.querySelectorAll('iframe');
for (var i = 0; i < iframes.length; i++) {
    iframes[i].src += 'something';
}

src属性是否始终相同?您可以通过以下方式进行选择:

var frame = $("iframe[src='www.mysite.com']"); 
编辑:

以下是获取和附加cookie的方法:

Document.cookie将以字符串形式返回整个cookie,我猜您可能只需要该cookie中的某个值。所以你可以这样做:

// This is just declaring a function that you can use to get cookie values. 
function getCookie(cookieName) {
    // If you open up your console and type in document.cookie, you will see
    // that all cookie values are inside of this same string. So this
    // function will allow you to extract the value you are looking for

    // We start by creating a new Regular Expression object, that will
    // be used to match a certain pattern of text.
    var re = new RegExp('[; ]'+cookieName+'=([^\\s;]*)');
    // This line actually uses our pattern, and returns the match from 
    // the cookie. 
    var sMatch = (' '+document.cookie).match(re);

    // if we have a cookie name, and our regular expression found a match
    // in the cookie, then return the value that was found from the match
    if (cookieName && sMatch) {
        return unescape(sMatch[1]);
    }

    // if we dont find a matching value, just return an empty string
    return '';
}
// Now we are actually CALLING our function, and pass in whatever we named
// our cookie value. The variable cookieValue will then hold this value. 
var cookieValue = getCookie("cookieName"); 

// Here we are using jQuery to find the iframe element we want to manipulate
// and save reference to it in a variable called 'frame'
var frame = $("iframe[src='www.mysite.com']"); 

// From this element, we grab it's current src value
var currentSrc = frame.attr("src"); 
// here, we SET the frames src value, by setting it back to it's current src
// and tack on the cookie value
frame.attr("src", currentSrc + "&" + cookieValue); 
它允许您执行以下操作:

// This is just declaring a function that you can use to get cookie values. 
function getCookie(cookieName) {
    // If you open up your console and type in document.cookie, you will see
    // that all cookie values are inside of this same string. So this
    // function will allow you to extract the value you are looking for

    // We start by creating a new Regular Expression object, that will
    // be used to match a certain pattern of text.
    var re = new RegExp('[; ]'+cookieName+'=([^\\s;]*)');
    // This line actually uses our pattern, and returns the match from 
    // the cookie. 
    var sMatch = (' '+document.cookie).match(re);

    // if we have a cookie name, and our regular expression found a match
    // in the cookie, then return the value that was found from the match
    if (cookieName && sMatch) {
        return unescape(sMatch[1]);
    }

    // if we dont find a matching value, just return an empty string
    return '';
}
// Now we are actually CALLING our function, and pass in whatever we named
// our cookie value. The variable cookieValue will then hold this value. 
var cookieValue = getCookie("cookieName"); 

// Here we are using jQuery to find the iframe element we want to manipulate
// and save reference to it in a variable called 'frame'
var frame = $("iframe[src='www.mysite.com']"); 

// From this element, we grab it's current src value
var currentSrc = frame.attr("src"); 
// here, we SET the frames src value, by setting it back to it's current src
// and tack on the cookie value
frame.attr("src", currentSrc + "&" + cookieValue); 

它的父母有身份证吗?您能再显示几层DOM树吗?可能父级有一个ID,您可以使用它直接访问这个iframe上面的一个div,它有一个class=container,但它有很多div。选择器有什么独特之处吗?不管它是否有几个div,你都可以用CSS选择器超级具体化,比如body.someClass someID.anotherClass.container>div>div>iFrame>指的是一个直系后代,只是试了一下。。。。你觉得这样行吗?不太确定。你的搜索线索功能没有任何作用。如果您想检索cookie值,请查看这个问题,以获得iframe的每个实例的一些帮助。因此,每页只有一个iframe,但有多个页面。每次加载新页面时,都需要附加iframe。尽管这可能对我有所帮助。。。我会尝试一些代码,然后再给你回复。我尝试的没有用。。。六羟甲基三聚氰胺六甲醚。。。我是一个相当新的程序员,只是想学习基础知识。如果我给你看看我做的饼干是什么样子的,会有帮助吗?我认为代码可以工作,但我仍然不明白如何将cookie作为字符串附加到src的末尾。在我用它选择它并附加我需要的东西之后,我是否必须重新加载iframe?不,你不应该这样做。它应该会自动重新加载。对不起,我只是刚刚开始编程。。。如何将cookie作为字符串附加到src的末尾?这是一个测试网站我的工作,我只是想了解基本知识。你看到我的编辑我的答案上面吗?我解释了一个获取cookie值并将其附加到iframe src的方法。您是否可以花点时间向我解释每一行的作用?对不起,就像我说的,我是新来的,我不完全明白。。。